I'm in a situation where I need to have my SQLMembershipProvider looking at two different databases. I'm able to set the connection string of the provider programmatically in the first instance, but am unable to change it for subsequent uses.
Does anyone know if this is possible and if so, how?
Thanks!
public class CustomSqlMembershipProvider : SqlMembershipProvider
{
public static string connectionString { get; set; }
public override void Initialize(string name, NameValueCollection config)
{
DatabaseConfig _config = DatabaseConfig.GetInstance();
if (String.IsNullOrEmpty(connectionString))
connectionString = _config.BuildConnectionString(DatabaseConfig.enumConnectionString.MyRedsourceDev);
config["connectionString"] = connectionString;
base.Initialize(name, config);
//Also tried this but it doesn't work
// Set private property of Membership provider.
//FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
//connectionStringField.SetValue(this, connectionString);
}
}
I can't help but think that the problem is because the Initialize method is only called once- when the user logs in. As far as I can see, the connection string is unable to change after this point, even if I set the connectionString property.
I have noticed that there is a private field called '_Initialzed' which I've tried to set to false using the same Reflection code above in the hope of forcing the provider to re-initialise each time, but to no avail
Right, I've sorted this now...
I've created a second SqlMembershipProvider (in both the web.config and as a class- like in the OP) but with a different connection string.
I then differentiate between the providers like so: