我的申请要求是单一申请&每个客户的多个数据库。所以我实现了自定义成员资格来实现这个首先我检查用户属于哪个客户端根据该客户端的连接字符串db我动态设置连接字符串&验证用户。
这是我的网络配置:
<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>
CustomMembership 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登录时,它会抛出此错误消息:
您的登录尝试未成功
当我再次运行应用程序&amp;我反之亦然。使用客户端B [DB2] -User2登录它成功登录但是当我在同一个应用程序运行中尝试使用客户端A [DB1] -User1时,它会抛出相同的错误。但我可以使用Client B [DB2]用户登录。
当我使用断点进行调试时,它会在
处抛出错误ValidateUser(string Username,string Passwd)
方法。 ValidateUser方法返回false。
我尝试了很多选项,但没有任何效果。请指导我。
答案 0 :(得分:0)
问题在于SqlMembership的Initialize方法。当用户第一次登录时,它会调用initialize方法&amp;设置连接字符串。当另一个用户尝试登录时,它使用相同的连接字符串。这就是为什么它没有验证。如果我们尝试调用SqlMembershipProvider的Initialize方法它会抛出&#34; InvalidOperationException的异常:成员资格提供程序已经初始化。&#34;
所以我创建了自定义成员资格的对象&amp;当我通过使用新创建的对象调用Membership validate方法时,我能够设置我的连接字符串。
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);
它成功运作。 Thanx to Shri n all ...: - )