首页 文章

.Net Core 1 Identity UserManager缓存的用户列表未更新

提问于
浏览
5

环境:.Net Core 1,EF,使用Identity进行身份验证,使用JWT令牌进行授权 .

遇到使用 UserManager.ChangePasswordAsync() 方法正确更新数据库的问题,但是没有更新 UserManager.Users 列表(假设) .

在Startup.cs中,我们只是使用 app.UseIdentity() ,而在 ApplicationUserService 构造函数中,我们正在注入 UserManager<ApplicationUser> . 除此之外,我们没有做任何习惯 .

例如:假设用户将其密码从“password1”更改为“password2” . 如果该用户注销并重新登录,则UserManager仍认为密码为“password1” . 如果我重新启动WebAPI服务器,则尝试登录;它可以像你期望的那样使用“password2” . 所以它肯定会更新数据库,但UserManager的范围/缓存没有更新 .

我想知道UserManager的默认DI范围是否是单例(而不是每个请求)?如果它没有更新UserStore的缓存用户列表,我可以看到导致此问题 .

有什么建议?需要更多代码?

ApplicationUserService(简化):

private readonly UserManager<ApplicationUser> _userManager;

public ApplicationUserService(UserManager<ApplicationUser> userManager)
{
     _userManager = userManager;
}

public Task<IdentityResult> ChangePasswordAsync(ApplicationUser user, string currentPassword, string newPassword)
{
    return _userManager.ChangePasswordAsync(user, currentPassword, newPassword);
}

[EDIT]

我不确定为什么会出现这种情况,但我刚才意识到,如果我将UserManager和SignInManager直接注入到Controller的构造函数中(而不是注入Service层),它似乎工作得很好 .

[EDIT 2]

调查结果摘要:

1)将UserManager和SignInManager注入到Service构造函数中,然后将该Service注入Controller构造函数不起作用 .

2) Injecting the UserManager and SignInManager into a Controller constructor works.

3)我还在Controller构造函数中测试了IServiceProvider的使用 . 我注入了IServiceProvider,然后使用GetService方法设置管理器: _userManager = serviceProvider.GetService<UserManager<ApplicationUser>>(); . 这与#1的结果相同 .

在#1和#3中:它将保存到数据库,但管理员似乎在以后使用时不知道数据更改 . 在这两种情况下,我都必须重新初始化应用程序(停止并启动服务器),以便更新缓存的数据 .

不应该#3和#2一样工作吗?

1 回答

  • 1

    [注意:以下代码不适用于Asp.Net Core . 要在Asp.Net Core中进行身份验证,请查看this documentation .

    我是Asp.Net的新手,但我尝试制作你所描述的内容 . 对我而言,它按预期工作 . 也许我的代码可以为你激发一个想法 .

    internal static void Main(string[] args)
        {
            _userStore = new UserStore<ApplicationUser>(new IdentityDbContext<ApplicationUser>());
            _userManager = new UserManager<ApplicationUser>(_userStore);
    
            var x = new ApplicationUser();
            x.UserName = "Test";
    
            foreach(string error in _userManager.Create(x, "password").Errors)
            {
                Console.WriteLine(error);
            }
    
            Console.WriteLine(_userManager.CheckPassword(x, "password"));
    
            var f = ChangePasswordAsync(x, "password", "pass12345");
            f.ContinueWith(delegate
            {
                if (f.IsFaulted)
                {
                    Console.WriteLine(f.Exception.Message);
                }
            }).ContinueWith(delegate
            {
                Console.WriteLine(_userManager.CheckPassword(x, "password"));
            });
    
            Console.ReadKey(true);
        }
    
        private static UserStore<ApplicationUser> _userStore;
    
        public class ApplicationUser : IdentityUser { }
    
        private static UserManager<ApplicationUser> _userManager;
    
        public static Task<IdentityResult> ChangePasswordAsync(ApplicationUser user, string currentPassword, string newPassword)
        {
            return _userManager.ChangePasswordAsync(user.Id, currentPassword, newPassword);
        }
    

相关问题