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

时间:2016-08-10 14:36:31

标签: c# asp.net-core identity usermanager

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

遇到使用UserManager.ChangePasswordAsync()方法的问题IS正确更新数据库,但不更新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);
}

[编辑]

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

[编辑2]

调查结果摘要:

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

2)将UserManager和SignInManager注入Controller构造函数。

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

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

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

1 个答案:

答案 0 :(得分: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); }

相关问题