使用Asp.net MVC和Identity2的动态数据库连接 [英] Dynamic database connection using Asp.net MVC and Identity2

查看:74
本文介绍了使用Asp.net MVC和Identity2的动态数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp.net mvc中开发一个Web应用程序. 我的应用程序使用多个数据库. 正在使用的数据库取决于登录的用户. 我在两个级别上管理登录:

I'm developing a web application in asp.net mvc. My application uses multiple databases. The database on which working on depends by the logged user. I manage login on two levels:

  • 主"数据库上的Level1,其中包含有关登录用户名/电子邮件和要使用的特定"数据库的信息.
  • 特定"数据库上的Level2,我使用Identity2管理用户和角色.

示例: 在主"数据库中,我在用户表中有一条记录,其中: -用户名= user1 -databaseToUse ="specificDb1"

Example: In the "master" database I have a record in User table with: - username = user1 - databaseToUse = "specificDb1"

在名为specificDb1的特定"数据库中,对于同一用户,我在User表中有一条记录,其中包含管理用户身份验证所需的全部内容.

In the "specific" database called specificDb1, for the same user, I have a record in User table with all I need to manage user authentication and more.

我想要实现的是:

  1. 启动网站,单击登录,输入用户名和密码,然后单击登录.
  2. 在主数据库中搜索用户名,如果存在,则获取与用户相关联的特定数据库名称.
  3. 在此处动态设置特定"数据库的连接字符串,并执行Identity 2登录操作.

第1点和第2点没有问题.问题在第3点. 我对(主数据库和特定数据库)数据库都使用EntityFramework 6 Code First.

No problems for points 1 and 2. The problem is in point 3. I use EntityFramework 6 Code First for both (master and specific) databases.

关于身份的配置部分,我在Startup.Auth.cs中看到:

Regarding the configuration part of Identity I see in Startup.Auth.cs:

        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

我应该更改身份配置中的某些内容吗?

Should I change something in Identity configuration?

谢谢.

推荐答案

花了几个小时在这里搜索我个人的(也许不是最好的)工作解决方案.

After hours spent in searching here my personal (maybe not the best) working solution.

在AccountController的Login操作中,首先检入"master"数据库后,在Session范围内设置特定"数据库信息:

In the Login action of AccountController, after the first check in "master" database, set the "specific" database informations in Session scope:

//Save the database infos in Session.
Session.Add("SqlServerInstance", masterUser.Company.DatabaseServer);
Session.Add("DbName", masterUser.Company.DatabaseName);

始终在AccountController中更新SignInManager和UserManager属性,以修复Identity上下文的连接字符串:

Always in AccountController update the SignInManager and UserManager properties fixing the connection string for the Identity context:

public ApplicationSignInManager SignInManager
    {
        get
        {
            //Set manually the right connection string used by the Identity database context.
            HttpContext.GetOwinContext().Get<ApplicationDbContext>().Database.Connection.ConnectionString = ApplicationDbContext.GetConnectionString();

            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            //Set manually the right connection string used by the Identity database context.
            HttpContext.GetOwinContext().Get<ApplicationDbContext>().Database.Connection.ConnectionString = ApplicationDbContext.GetConnectionString();

            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

最后是为我们提供连接字符串的方法:

And finally the method that give us the connection string:

/// <summary>
    /// Get the connection string getting SqlServerInstance and DbName from Session.
    /// </summary>
    public static string GetConnectionString()
    {

        string sqlServerInstance = DEFAULT_SQLSERVERINSTANCE;
        if (HttpContext.Current.Session != null && HttpContext.Current.Session["SqlServerInstance"] != null)
            sqlServerInstance = Convert.ToString(HttpContext.Current.Session["SqlServerInstance"]);

        string dbName = DEFAULT_DBNAME;
        if (HttpContext.Current.Session != null && HttpContext.Current.Session["DbName"] != null)
            dbName = Convert.ToString(HttpContext.Current.Session["DbName"]);

        return "Data Source=" + sqlServerInstance + ";Initial Catalog=" + dbName + ";Integrated Security=True";

    }

希望这会有所帮助.

这篇关于使用Asp.net MVC和Identity2的动态数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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