【发布时间】:2016-07-02 02:54:52
【问题描述】:
我有
Controller 和 IActionResult 如下
public HomeController(
UserManager<CustomerApplicationUser> userManager,
SignInManager<CustomerApplicationUser> signInManager,
IMemoryCache cache)
{
_userManager = userManager;
_signInManager = signInManager;
_cache = cache;
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
var companyResult = new Company();
using (var GeneralCompanyContext = new GeneralCompanyContext())
{
companyResult = await GeneralCompanyContext.Company.FirstOrDefaultAsync(t => t.CompanyNumber == model.CompanyNumber);
}
using (var context = new CustomerContext(companyResult.ConnectionString))
{
var LoginResult = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, lockoutOnFailure: false);
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework().AddSqlServer()
.AddDbContext<CustomerContext>()
.AddDbContext<GeneralCompanyContext>();
services.AddIdentity<CustomerApplicationUser, IdentityRole>().AddEntityFrameworkStores<CustomerContext>().AddDefaultTokenProviders();
services.AddIdentity<GeneralCompanyApplicationUser, IdentityRole>().AddEntityFrameworkStores<GeneralCompanyContext>().AddDefaultTokenProviders();
}
CustomerContext 类:
CustomerContext : : IdentityDbContext<CustomerApplicationUser>
{
private readonly string _ConnectionString;
public CustomerContext()
{
_ConnectionString = Configuration["Data:DefaultConnection:MigrationConnectionString"]).ToString();
}
public CustomerContext(string ConnectionString)
{
_ConnectionString = ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_ConnectionString);
base.OnConfiguring(optionsBuilder);
}
}
问题:
我有 2 个 dbcontext 作为 CustomerContext 和 GeneralCompanyContext。
CustomerContext 有客户数据库。
GeneralCompanyContext 拥有所有客户的所有数据库连接字符串。
项目何时开始
services.AddEntityFramework().AddSqlServer() 从
添加连接字符串配置["Data:DefaultConnection:MigrationConnectionString"]
自动。所以当我尝试登录 Index Iactionresult 时,signinmanager 连接字符串总是显示 Configuration["Data:DefaultConnection:MigrationConnectionString"] 连接字符串。
如何从 companyResult.ConnectionString 为 usermanager(asp.net 身份) 设置连接字符串?
任何帮助将不胜感激。
谢谢。
【问题讨论】:
标签: asp.net-identity asp.net-core-mvc