小编Mat*_* H.的帖子

ASP.NET核心身份 - 扩展密码哈希

我正在努力将应用程序从Web窗体移动到MVC,并选择使用ASP.NET Core来使用MVC 6.

在我目前的应用程序中,我有一个与Identity一起使用的自定义密码.在我的自定义UserManager类中实现非常简单:

public ApplicationUserManager()
  : base(new UserStore<IdentityUser>(new AuthContext()))
{
    this.PasswordHasher = new SqlPasswordHasher();
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用.NET Core执行相同操作,但UserManager中不存在PasswordHasher属性.我看到构造函数将采用IPasswordHasher参数,所以我尝试了这个:

public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators,
        IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer,
        IdentityErrorDescriber errors, IServiceProvider serviceProvider, ILogger<UserManager<ApplicationUser>> logger)
  : base(store, optionsAccessor, new SqlPasswordHasher(), userValidators, passwordValidators, keyNormalizer, errors,
        serviceProvider, logger)
{
}
Run Code Online (Sandbox Code Playgroud)

在SqlPasswordHasher中,我只是重写了VerifyHashedPassword方法,如下所示:

public override PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword)
{
    // My custom logic is here
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是,上述方法不起作用.我在SqlPasswordHasher的VerifyHashedPassword方法中设置了断点,但它没有被触发.

我以为我是以错误的方式解决这个问题,我应该利用DI来实现这个目标.我更新了我的用户管理器的构造函数,以便它不实例化新的SqlPasswordHasher,而是使用默认的interface参数:

public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor,
        IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-mvc asp.net-identity-3 asp.net-core

6
推荐指数
1
解决办法
3730
查看次数