ASP.NET Core中的密码重置令牌提供程序-找不到IUserTokenProvider [英] Password reset token provider in ASP.NET core - IUserTokenProvider not found

查看:72
本文介绍了ASP.NET Core中的密码重置令牌提供程序-找不到IUserTokenProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我对Google进行了详尽的搜索,但是有来自ASP.NET的数百个示例,但与ASP.NET Core无关.

I googled thoroughly but there is hundred examples from ASP.NET but nothing about ASP.NET Core.

为了进行密码重置工作,我需要将IUserTokenProvider实例注册到DI中.

In order to getting password reset work I need to register an IUserTokenProvider instance into DI.

没有它,我在下面的行出现异常:

Without it I get exception at following line:

var result = await _userManager.ResetPasswordAsync(user, token, password);

例外

"No IUserTokenProvider named 'PasswordResetTokenProvider' is registered."

这很有意义,所以我尝试在DI中注册它:

That makes sense so I tried to register it in the DI:

services.AddSingleton<IUserTokenProvider<User>, DataProtectorTokenProvider<User>>();

但是接口 IUserTokenProvider 不存在.我在文件中使用了 Microsoft.AspNetCore.Identity .它甚至在项目gitlab中都不存在.

But the interface IUserTokenProvider does not exists. I'm using Microsoft.AspNetCore.Identity in the file. It even does not exists in project gitlab.

好吧,在挖掘身份源代码后,我发现了一个相似接口 IUserTwoFactorTokenProvider< T> .让我们改用它:

Well, after digging in identity source code I find a similar interface IUserTwoFactorTokenProvider<T>. Let's use this instead:

services.AddSingleton<IUserTwoFactorTokenProvider<User>, DataProtectorTokenProvider<User>>();

...也没有运气.

TL; DR:

请-如何在ASP.NET 核心中重置密码工作?充其量举个例子.

Please - how to get the password reset in ASP.NET Core work? At best with some example.

谢谢.

推荐答案

我不确定这是解决方法还是常规方法,但是 IUserTwoFactorTokenProvider 接口似乎是正确的方法.IUserTokenProvider似乎不再存在.

I'm not sure if it's workaround or normal approach, but the IUserTwoFactorTokenProvider interface seems to be a right way. IUserTokenProvider appears to no longer exists.

弄清楚我必须以身份手动注册提供者:

Figured out that I have to register the provider manually in the identity:

services.AddIdentity<User, Role>(options =>
            {
                ...
                options.Tokens.ProviderMap.Add("Default", new TokenProviderDescriptor(typeof(IUserTwoFactorTokenProvider<User>)));
            })

以及 ConfigureServices 中的可选配置:

services.Configure<DataProtectionTokenProviderOptions>(o =>
        {
            o.Name = "Default";
            o.TokenLifespan = TimeSpan.FromHours(1);
        });

并且密码重置/电子邮件验证令牌现在可以正常工作.

And the password reset / email validation tokens are working now.

PS:打开了一个需要澄清的问题

这篇关于ASP.NET Core中的密码重置令牌提供程序-找不到IUserTokenProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆