My requirement of application is single application & multiple databases for each client. So I implemented custom membership to achieve this. First I check user belongs to which Client the according to connection string of that client's db I dynamically set connection string & validate user.
Here is my web config :
<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;
}
}
}
}
When I runs my application locally when I login with Client A[DB1]-User1 is logins successfully. But when I logout and trying to login with Client B [DB2] User2 it throws this error message :
Your login attempt was not successful
When I again run the application & I do vice versa ie. login with Client B[DB2]-User2 it login successfully but when I try with Client A[DB1]-User1 in the same application run it throws the same error. But I am able to login with Client B[DB2] users.
When I debugged with breakpoints it throws error at
ValidateUser(string Username, string Passwd)
method. ValidateUser method returning false.
I have tried lots of options but nothing works. Please guide me through.
The problem was with SqlMembership's Initialize method. When user logs in first time it calls initialize method & set connection string. When another user tries to login it uses the same connection string. that's why its not validating. If we try to call SqlMembershipProvider's Initialize method It throws an exception of "InvalidOperationException : membership provider already initialized."
So I created the object of my custom membership & when I call Membership validate method by using newly created object I am able to set my connection string.
And it worked successfully. Thanx to Shri n all... :-)