我有3个数据库,一个GlobalDb用于根据登录详细信息管理和重定向用户正确的数据库。
其他2个数据库与GlobalDb不同,但彼此相同。 让我们称这两个数据库为Company1和Company2。
GlobalDb不需要身份框架,所以它只是通过使用标准的EntityFramework连接来连接,另外2个确实需要Identity框架。
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;
}
}
答案 0 :(得分: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("");
}