【问题标题】:C# MVC ASP.NET Identity - Dynamically change ApplicationDbContext connection string during runtimeC# MVC ASP.NET Identity - 在运行时动态更改 ApplicationDbContext 连接字符串
【发布时间】:2015-11-09 10:29:35
【问题描述】:

我有 3 个数据库,一个 GlobalDb 用于根据登录详细信息管理和重定向用户正确的数据库。

其他 2 个数据库与 GlobalDb 不同,但彼此相同。 让我们将这两个数据库称为 Company1 和 Company2。

GlobalDb 不需要身份框架,因此它只是通过使用标准 EntityFramework 连接进行连接,其他 2 个确实需要身份框架。

3 个连接字符串保存在 web.config 中。

我可以正确查询所有数据库,并且它们都返回数据。 我遇到的主要问题是登录时,我如何告诉 SignInManager 数据库使用哪个连接字符串?

IdentityModels.cs

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

    public static CompanyDb Create(string CompanyName)
    {
        return new CompanyDb(CompanyName);
    }

    // Virtual class and table mappings go here.
}

AccountController 登录

public async Task<ActionResult> CustomLogin(string Email, string Password, string returnUrl)
    {
        GlobalDb Global = new GlobalDb();

        // check what company the user belongs to based on email.
        var CompanyName = Global.Users.Where(u => u.Email == Email && u.Active == true).Select(u => u.Company).FirstOrDefault();

        // Connect to the desired database to get test data.
        CompanyDb Company = new CompanyDb(CompanyName);
        var DataTest = Company.Users.ToList();


        if (CompanyName != null)
        {
            var result = await SignInManager.PasswordSignInAsync(Email, Password, false, shouldLockout: false); // <-- How to connect to the correct DB?
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View("");
            }
        }

        return View("");
    }

我在以下文件和函数中不断得到一个空值

IdentityModel.cs

 // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

【问题讨论】:

  • 好的,'var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie)' 错误是由一个愚蠢的错误引起的,我的用户表中的 SecurityStamp 为空。 link,但我仍然无法让登录管理器将我指向正确的数据库,请帮助。

标签: asp.net-mvc entity-framework connection-string asp.net-identity


【解决方案1】:

我想通了。

如果只是将 SignInManager 包装在 Context Using 语句中,则在我的登录操作中,如下所示。

public async Task<ActionResult> CustomLogin(string Email, string Password, string returnUrl)
    {
        GlobalDb Global = new GlobalDb();

        // check what company the user belongs to based on email.
        var CompanyName = Global.Users.Where(u => u.Email == Email && u.Active == true).Select(u => u.Company).FirstOrDefault();

        if (CompanyName != null)
        {
            using (CompanyDb db = new CompanyDb(CompanyName))
            {
                var result = await SignInManager.PasswordSignInAsync(Email, Password, false, shouldLockout: false); // <-- How to connect to the correct DB?
                switch (result)
                {
                    case SignInStatus.Success:
                        return RedirectToLocal(returnUrl);
                    case SignInStatus.LockedOut:
                        return View("Lockout");
                    case SignInStatus.RequiresVerification:
                        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
                    case SignInStatus.Failure:
                    default:
                        ModelState.AddModelError("", "Invalid login attempt.");
                        return View("");
                }
            }

        }

        return View("");
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2016-08-17
    • 1970-01-01
    • 2021-12-18
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多