Hello Everyone,
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>
My CustomMembership Class :
public class EPMembershipProvider : SqlMembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
Int64 ClientId = Convert.ToInt64(HttpContext.Current.Session["_clientId"]);
if (ClientId > 0)
{
UserLoginML objUL = new UserLoginML();
string _dbMembershipConnStr = HttpContext.Current.Session["_dbMembershipConnStr"].ToString();
if (_dbMembershipConnStr != "" && _dbMembershipConnStr != "db_not_validate" && _dbMembershipConnStr != "db_not_exist")
{
try
{
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
if (connectionStringField != null)
connectionStringField.SetValue(this, _dbMembershipConnStr);
var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (roleField != null)
roleField.SetValue(Roles.Provider, _dbMembershipConnStr);
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 &I login with Client A[DB1]-User1 then it 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.
It means the users of particular client's db are able to login if very first time I logs in with any user of that db. And I once logged in other db users are not able to login.
When I debugged with breakpoints it returns false in :
bool ValidateUser(string Username, string Passwd).
I have checked the users are not locked out or inactive. And Membership uses associated connection string as per user.
If anybody goes from this strange problem & help me through.