如何在每个页面请求中更改asp.net成员资格的数据库? [英] How to change database for asp.net membership with every page request?

查看:49
本文介绍了如何在每个页面请求中更改asp.net成员资格的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方案,其中我的应用程序针对url中的子域连接到不同的数据库.

我已经尝试过以下代码:

 公共类Provider:SqlMembershipProvider{公共重写void Initialize(字符串名称,System.Collections.Specialized.NameValueCollection配置){base.Initialize(name,config);//更新基类中的私有连接字符串字段.SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();sqlBuilder [数据源"] ="My-PC \\ SQLEXPRESS";字符串dbName;如果(!string.IsNullOrEmpty(Helpers.RouteManager.GetSubDomain())){dbName = Helpers.RouteManager.GetSubDomain().ToString()+"_ db";}别的{dbName ="dbName";}bool isEqual = string.Equals(name,dbName);sqlBuilder [初始目录"] = dbName;sqlBuilder [集成安全性"] = true;sqlBuilder [应用程序名称"] ="EntityFramework";FieldInfo connectionStringField = GetType().BaseType.GetField("_ sqlConnectionString",BindingFlags.Instance | BindingFlags.NonPublic);connectionStringField.SetValue(this,sqlBuilder.ConnectionString);}} 

我正在从 Application_BeginRequest()方法调用此函数.

我面临的问题是,当我构建应用程序时,当它命中具有特定子域的url时,它会连接到指定的数据库,但是我尝试使用其他子域,它仍然具有相同的asp.net成员身份数据库./p>

但是当我重建应用程序并尝试另一个子域时,它会起作用.

在构建解决方案之后,似乎我的代码仅在第一次工作.

请有人指导我.

感谢

解决方案

抱歉,这并不是真正的答案,但我想在这里质疑您的体系结构.为什么需要您的应用程序将成员数据包含在多个数据库中?本质上是为整个应用程序维护帐户详细信息(身份验证/授权).如果您希望能够根据Url控制对应用程序某些区域的访问,则可以使用基于角色的表单身份验证来实现.互联网上有很多文章对此进行了解释.希望这能使您朝正确的方向前进.

更新:我刚刚在stackoverflow上找到了这个答案.恐怕您想做的事情似乎是不可能的,因为成员资格提供程序是应用程序的所有用户之间共享的全局静态对象.根据url进行更改不仅会针对特定用户进行更改,还会针对所有登录用户进行更改在此处查看

I have a scenario in which my application connects to different databases with respect to subdomains in 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.

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.

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

解决方案

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. 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. 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

这篇关于如何在每个页面请求中更改asp.net成员资格的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆