我正在使用ASP.NET Identity和EF 6.2 . 我的项目是定制用户管理存储更改用户密码 . 我的代码参考链接:https://blogs.msdn.microsoft.com/webdev/2014/01/06/implementing-custom-password-policy-using-asp-net-identity

所以我添加了一个新的表AspNetCustomUserPreviousPasswords来存储用户更改密码:

public class AspNetCustomUserPreviousPasswords { public AspNetCustomUserPreviousPasswords() { CreatedDate = DateTime.Now; } DatabaseGenerated(DatabaseGeneratedOption.Identity), Key, Column(Order = 0) public int Id { get; set; } [Column(Order = 1)] [MaxLength(128)] [Required] public string HashPassword { get; set; } [Column(Order = 2)] [Required] public DateTime CreatedDate { get; set; } [Key, Column(Order = 3)] public string UserId { get; set; } public virtual ApplicationUser User { get; set; } }

然后我在ApplicationUser.cs中更新了我的代码以使用Identity DB Context并管理用户请求密码:
public class ApplicationUser : IdentityUser { public ApplicationUser() : base() { UserPreviousPasswords = new List<AspNetCustomUserPreviousPasswords>(); } public virtual IList UserPreviousPasswords { get; set; } public async Task GenerateUserIdentityAsync(UserManager manager, string authenticationType) { var userIdentity = await manager.CreateIdentityAsync(this, authenticationType); // Add custom user claims here return userIdentity; } } public override async Task ChangePasswordAsync(string userId, string currentPassword, string newPassword) { if (await IsPreviousPassword(userId, newPassword)) { return await Task.FromResult(IdentityResult.Failed("Cannot reuse old password")); } var result = await base.ChangePasswordAsync(userId, currentPassword, newPassword); if (result.Succeeded) { var store = Store as ApplicationUserStore; await store.AddToPreviousPasswordsAsync(await FindByIdAsync(userId), PasswordHasher.HashPassword(newPassword)); } return result; } private async Task IsPreviousPassword(string userId, string newPassword) { var user = await FindByIdAsync(userId); if (user.UserPreviousPasswords.OrderByDescending(x => x.CreatedDate). Select(x => x.HashPassword).Take(PASSWORD_HISTORY_LIMIT).Where(x => PasswordHasher.VerifyHashedPassword(x, newPassword) != PasswordVerificationResult.Failed). Any()) { return true; } return false; }

所以,在任务IsPreviousPassword var user = await FindByIdAsync(userId);
user var不会从属性UserPreviousPasswords加载密码:
enter image description here

我错了什么?我需要在找到用户时在UserPreviousPasswords中加载历史密码 .