简体   繁体   English

如何在每个页面请求中更改asp.net成员资格的数据库?

[英]How to change database for asp.net membership with every page request?

I have a scenario in which my application connects to different databases with respect to subdomains in url. 我有一个方案,其中我的应用程序针对url中的子域连接到不同的数据库。

I have tried this code: 我已经试过这段代码:

 public class Provider : 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.

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder["Data Source"] = "My-PC\\SQLEXPRESS";
        string dbName;
        if (!string.IsNullOrEmpty(Helpers.RouteManager.GetSubDomain()))
        {
            dbName = Helpers.RouteManager.GetSubDomain().ToString()+"_db";
        }
        else
        {
            dbName = "dbName";
        }
        bool isEqual = string.Equals(name, dbName);
        sqlBuilder["Initial Catalog"] = dbName;
        sqlBuilder["Integrated Security"] = true;
        sqlBuilder["Application Name"] = "EntityFramework";
        FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
        connectionStringField.SetValue(this, sqlBuilder.ConnectionString);
    }
}

And I am calling this function from Application_BeginRequest() method. 我从Application_BeginRequest()方法调用此函数。

The issue I am facing is that when I build application and it hits url with specific sub-domain, it connects to the specified database but I try different sub-domain it sticks with the same database for asp.net membership. 我面临的问题是,当我构建应用程序并以特定的子域访问url时,它会连接到指定的数据库,但是我尝试使用其他子域,而该子域坚持使用相同的数据库作为asp.net成员。

But when i rebuild the application and try another sub domain it works. 但是,当我重建应用程序并尝试另一个子域时,它可以工作。

It seems that my code is working only for the first time after I build the solution. 在构建解决方案后,似乎我的代码仅在第一次工作。

Please someone guide me. 请有人指导我。

Thanx 感谢名单

Try to configure different Membership providers in web config file. 尝试在Web配置文件中配置其他成员资格提供程序。

<membership>
    <providers>
        <add name="ProviderA" connectionStringName="ConnectionA" ... />
        <add name="ProviderB" connectionStringName="ConnectionB" ... />
    </providers>
</membership>

<connectionStrings>
     <add name="ConnectionA" .... />
     <add name="ConnectionB" .... />
</connectionStrings>

And then use this providers in code 然后在代码中使用此提供程序

var user = Membership.Providers["ProviderA"].GetUser(username, false);
var user = Membership.Providers["ProviderB"].GetUser(username, false);

sorry this isn't really an answer but I'd like to question your architecture here. 抱歉,这不是真正的答案,但我想在这里质疑您的体系结构。 Why do you need your application to contain membership data in multiple databases? 为什么需要您的应用程序将成员数据包含在多个数据库中? It's nature is to maintain account details (authentication/authorization) for your application as a whole. 本质上是为整个应用程序维护帐户详细信息(身份验证/授权)。 If you want the ability to control access to certain areas of your application based on the Url you could do this by using Role based Forms Authentication. 如果您希望能够根据Url控制对应用程序某些区域的访问,则可以使用基于角色的表单身份验证来实现。 There are lots of articles on the interweb explaining this. 互联网上有很多文章对此进行了解释。 Hope this sets you off on the right direction. 希望这能使您朝正确的方向前进。

Update: I've just found this answer on stackoverflow. 更新:我刚刚在stackoverflow上找到了这个答案。 I'm afraid it seems what you want to do is not possible as the membership provider is a global static object shared between all users of your application. 恐怕似乎无法实现,因为成员资格提供程序是应用程序的所有用户之间共享的全局静态对象。 Changing it based on a url would not just change it for that particular user but for everyone that is logged in check it out here 根据网址进行更改不仅会针对特定用户进行更改,还会针对所有登录用户进行更改, 请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM