[英]Using ASP.Net SqlMembershipProvider with multiple databases
I'm in a situation where I need to have my SQLMembershipProvider looking at two different databases. 我处于一种需要让我的SQLMembershipProvider查看两个不同数据库的情况。 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. 我忍不住想到了问题所在,因为初始化登录方法仅在用户登录时被调用一次。据我所知,即使设置了connectionString,连接字符串在此之后也无法更改。属性。
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 我注意到有一个名为“ _Initialzed”的私有字段,我尝试使用上面的相同反射代码将其设置为false,以期希望每次都强制提供程序重新初始化,但无济于事
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. 我已经创建了第二个SqlMembershipProvider(在web.config和OP中都作为类),但是具有不同的连接字符串。
I then differentiate between the providers like so: 然后,我像这样区分提供者:
MembershipUser user = Membership.Providers["SPECIFICPROVIDERNAME"].GetUser(username, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.