【问题标题】:AspNetCore Identity Update DbContext At RuntimeAspNetCore Identity 在运行时更新 DbContext
【发布时间】: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


    【解决方案1】:

    解决方案:

    public class APIController : ControllerBase
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly IOptions<IdentityOptions> _identityOptions;
        private readonly IServiceProvider _serviceProvider;
        private readonly ILogger<UserManager<ApplicationUser>> _logger;
    
        public APIController(
            UserManager<ApplicationUser> userManager,
            IOptions<IdentityOptions> identityOptions,
            IServiceProvider serviceProvider,
            ILogger<UserManager<ApplicationUser>> logger)
        {
            _userManager = userManager
            _identityOptions = identityOptions;
            _serviceProvider = serviceProvider;
            _logger = logger;
        }
    
        public async Task<IActionResult> Authenticate([FromBody] UserLoginModel model)
        {
    
            // Detect Customer's Database
            DbContext dbContext = GetCustomersDbContext(model.UserName);
    
            // Solution
            UserStore<ApplicationUser> store = new(dbContext);
            UserManager<ApplicationUser> uManager = new(store,
                    _identityOptions,
                    _userManager.PasswordHasher,
                    _userManager.UserValidators,
                    _userManager.PasswordValidators,
                    _userManager.KeyNormalizer,
                    _userManager.ErrorDescriber,
                    _serviceProvider,
                    _logger);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2015-12-28
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      相关资源
      最近更新 更多