我正面临 "Your login attempt was not successful" 请重试。实现自定义成员资格时出错
I am facing "Your login attempt was not successful" Please try again. error while implementing custom membership
我对应用程序的要求是每个客户端的单个应用程序和多个数据库。所以我实现了自定义成员资格来实现这一点。首先,我根据该客户端数据库的连接字符串检查用户属于哪个客户端,我动态设置连接字符串并验证用户。
这是我的网络配置:
<membership defaultProvider="EPMembershipProvider" userIsOnlineTimeWindow="20">
<providers>
<clear />
<add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider" connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
<!--<add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider, EfficientPeople" connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />-->
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DBEntitiesConnectionString" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="DBEntitiesConnectionString" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
自定义会员 Class :
public class EPMembershipProvider : SqlMembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// Update the private connection string field in the base class.
Int64 ClientId = Convert.ToInt64(HttpContext.Current.Session["_clientId"]);
if (ClientId > 0)
{
UserLoginML objUL = new UserLoginML();
//string _dbMembershipConnStr = ManageSessionFacade._dbMembershipConnStr;
string _dbMembershipConnStr = HttpContext.Current.Session["_dbMembershipConnStr"].ToString();
//string connectionString = ManageSessionVariable.Current._dbContextConnStr;
if (_dbMembershipConnStr != "" && _dbMembershipConnStr != "db_not_validate" && _dbMembershipConnStr != "db_not_exist")
{
try
{
// Set private property of Membership provider.
//FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
//connectionStringField.SetValue(this, _dbMembershipConnStr);
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
if (connectionStringField != null)
connectionStringField.SetValue(this, _dbMembershipConnStr);
//Set role provider
var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (roleField != null)
roleField.SetValue(Roles.Provider, _dbMembershipConnStr);
//Set Profile Provider
var profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (profileField != null)
profileField.SetValue(ProfileManager.Provider, _dbMembershipConnStr);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
当我 运行 在本地使用客户端 A 登录我的应用程序时 [DB1]-User1 登录成功。但是当我注销并尝试使用客户端 B [DB2] User2 登录时,它会抛出此错误消息:
Your login attempt was not successful
当我再次 运行 应用程序时,反之亦然。使用客户端 B[DB2]-User2 登录成功,但是当我在同一应用程序 运行 中尝试使用客户端 A[DB1]-User1 时,它会抛出相同的错误。但是我可以用客户端 B[DB2] 用户登录。
当我使用断点进行调试时,它在
处抛出错误
ValidateUser(string Username, string Passwd)
方法。返回 false 的 ValidateUser 方法。
我尝试了很多选项,但没有任何效果。请指导我。
问题出在 SqlMembership 的 Initialize 方法上。当用户第一次登录时,它调用初始化方法并设置连接字符串。当另一个用户尝试登录时,它使用相同的连接字符串。这就是为什么它没有验证。如果我们尝试调用 SqlMembershipProvider 的 Initialize 方法,它会抛出 "InvalidOperationException : membership provider already initialized."
的异常
所以我创建了我的自定义成员身份的对象,当我使用新创建的对象调用成员资格验证方法时,我能够设置我的连接字符串。
EPMembershipProvider myProvider = new EPMembershipProvider();
string configPath = "~/web.config";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
MembershipSection section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
myProvider.Initialize("EPMembershipProvider", membershipParams);
bool Status = myProvider.ValidateUser(UserName, Password);
并且成功运行。感谢 Shri n 所有... :-)
我对应用程序的要求是每个客户端的单个应用程序和多个数据库。所以我实现了自定义成员资格来实现这一点。首先,我根据该客户端数据库的连接字符串检查用户属于哪个客户端,我动态设置连接字符串并验证用户。
这是我的网络配置:
<membership defaultProvider="EPMembershipProvider" userIsOnlineTimeWindow="20">
<providers>
<clear />
<add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider" connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
<!--<add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider, EfficientPeople" connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />-->
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DBEntitiesConnectionString" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="DBEntitiesConnectionString" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
自定义会员 Class :
public class EPMembershipProvider : SqlMembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// Update the private connection string field in the base class.
Int64 ClientId = Convert.ToInt64(HttpContext.Current.Session["_clientId"]);
if (ClientId > 0)
{
UserLoginML objUL = new UserLoginML();
//string _dbMembershipConnStr = ManageSessionFacade._dbMembershipConnStr;
string _dbMembershipConnStr = HttpContext.Current.Session["_dbMembershipConnStr"].ToString();
//string connectionString = ManageSessionVariable.Current._dbContextConnStr;
if (_dbMembershipConnStr != "" && _dbMembershipConnStr != "db_not_validate" && _dbMembershipConnStr != "db_not_exist")
{
try
{
// Set private property of Membership provider.
//FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
//connectionStringField.SetValue(this, _dbMembershipConnStr);
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
if (connectionStringField != null)
connectionStringField.SetValue(this, _dbMembershipConnStr);
//Set role provider
var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (roleField != null)
roleField.SetValue(Roles.Provider, _dbMembershipConnStr);
//Set Profile Provider
var profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (profileField != null)
profileField.SetValue(ProfileManager.Provider, _dbMembershipConnStr);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
当我 运行 在本地使用客户端 A 登录我的应用程序时 [DB1]-User1 登录成功。但是当我注销并尝试使用客户端 B [DB2] User2 登录时,它会抛出此错误消息:
Your login attempt was not successful
当我再次 运行 应用程序时,反之亦然。使用客户端 B[DB2]-User2 登录成功,但是当我在同一应用程序 运行 中尝试使用客户端 A[DB1]-User1 时,它会抛出相同的错误。但是我可以用客户端 B[DB2] 用户登录。
当我使用断点进行调试时,它在
处抛出错误ValidateUser(string Username, string Passwd)
方法。返回 false 的 ValidateUser 方法。
我尝试了很多选项,但没有任何效果。请指导我。
问题出在 SqlMembership 的 Initialize 方法上。当用户第一次登录时,它调用初始化方法并设置连接字符串。当另一个用户尝试登录时,它使用相同的连接字符串。这就是为什么它没有验证。如果我们尝试调用 SqlMembershipProvider 的 Initialize 方法,它会抛出 "InvalidOperationException : membership provider already initialized."
的异常所以我创建了我的自定义成员身份的对象,当我使用新创建的对象调用成员资格验证方法时,我能够设置我的连接字符串。
EPMembershipProvider myProvider = new EPMembershipProvider();
string configPath = "~/web.config";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
MembershipSection section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
myProvider.Initialize("EPMembershipProvider", membershipParams);
bool Status = myProvider.ValidateUser(UserName, Password);
并且成功运行。感谢 Shri n 所有... :-)