【发布时间】:2021-06-21 02:03:30
【问题描述】:
应用:
- .NET 5 Web API
- 每个客户一个 sql 数据库
- 智威汤逊
问题:
有一个名为 'APIController' 的控制器使用 'Authenticate' 方法。 此方法从正文接收“用户名”和“密码”。
通过这些参数,我能够检测到客户的数据库,但我不知道如何将数据库(dbContext)设置到我通过依赖注入获得的 userManager 中。
public class APIController : ControllerBase
{
private readonly UserManager<ApplicationUser> _userManager;
public APIController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager
}
public async Task<IActionResult> Authenticate([FromBody] UserLoginModel model)
{
// Detect Customer's Database
DbContext dbContext = GetCustomersDbContext(model.UserName);
/*
Problem:
Check if the password is correct in the database I just found.
How can I set the dbContext in _userManager?
What I was trying to do:
var store = new UserStore<ApplicationUser>(dbContext);
var userManager = new UserManager<ApplicationUser>(store,
_userManager.Options,
_userManager.PasswordHasher,
_userManager.UserValidators,
_userManager.PasswordValidators,
_userManager.KeyNormalizer,
_userManager.ErrorDescriber,
_serviceProvider,
_logger);
*/
}
}
Startup.cs
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.User.RequireUniqueEmail = false;
})
.AddEntityFrameworkStores<FileWebDbContext>()
.AddDefaultTokenProviders();
很多小时试图找到一个解决方案,如果有人给我任何想法,将不胜感激!
更新解决方案below
【问题讨论】:
标签: c# asp.net-core asp.net-identity asp.net-core-identity