简体   繁体   English

将ASP.Net SqlMembershipProvider与多个数据库一起使用

[英]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.

相关问题 将现有的SqlMembershipProvider数据库与ASP.NET MVC应用程序一起使用 - Using an existing SqlMembershipProvider database with an ASP.NET MVC app 使用多个数据库的单个ASP.NET MVC应用程序 - Single ASP.NET MVC application using multiple databases 在哪里可以使用ASP.NET MVC和SqlMembershipProvider存储其他用户详细信息? - Where do I store additional user details using ASP.NET MVC and the SqlMembershipProvider? 具有多个数据库的MVC 5 Asp.Net身份 - MVC 5 Asp.Net Identity with multiple databases ASP.NET Boilerplate 多个数据库和 DbContexts - ASP.NET Boilerplate multiple databases and DbContexts Asp.net MVC2-多个数据库 - Asp.net MVC2 - Multiple databases ASP.NET Core Web API - 使用Sybase和SQL Server实现多数据库 - ASP.NET Core Web API - implement multiple databases using Sybase and SQL Server 对多个数据库或解决方案使用ASP.net用户和角色表...可以吗? 如果是,怎么办? - Using ASP.net Users and roles table for multiple databases or solutions… is it possible? if yes how? 在ASP.NET MVC应用程序中管理多个客户数据库 - Managing multiple customer databases in ASP.NET MVC application 使用多个数据库的ASP.NET API服务 - ASP.NET API service working with multiple databases
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM