从单个数据库迁移到多租户数据库身份验证

时间:2016-02-10 17:41:27

标签: .net sql-server asp.net-mvc asp.net-identity owin

我的ASP.NET MVC应用程序最初是使用OWIN身份验证为单个SQL Server数据库开发的,其中身份表和数据表位于同一个数据库中。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("defaultConnectionString", throwIfV1Schema: false)
    {
    }
}

我们现在正在迁移代码以支持多租户单独的数据库模型。在此模型中,连接字符串在用户输入凭据(useridpasswordcompanyId)后决定。

companyId是要使用的连接字符串的区分因素。这里选择数据连接字符串没有问题,问题是在用户登录后设置IdentityDbContext<ApplicationUser>而不是Startup:Configuration

这是我的ConfigureAuth课程:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

//....

我为ApplicationDbContext尝试了一个新的构造函数,它使用&#34; mydb&#34;参数

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(string mydb)
        : base(mydb, throwIfV1Schema: false)
    {
    }

但是这在Startup上没用,因为那时没有登录凭据。我在想是否有办法重新初始化app.CreatePerOwinContext(ApplicationDbContext.Create);并传递&#34; mydb&#34;参数因此它将它用于身份存储,而不是defaultConnectionString,这可以解决问题。

我的目标是在每个租户的同一数据库中包含身份表和数据表,并在用户登录后决定数据库。

提前谢谢!

0 个答案:

没有答案
相关问题