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:打开了一个需要澄清的问题