Password reset token provider in ASP.NET core - IUserTokenProvider not found

rudolfdobias picture rudolfdobias · Nov 6, 2016 · Viewed 13.9k times · Source

Hello,

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

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);

Exception

"No IUserTokenProvider named 'PasswordResetTokenProvider' is registered."

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

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

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

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>>();

... and no luck.


TL;DR:

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

Thank you in advance.

Answer

Jeremy Lakeman picture Jeremy Lakeman · Apr 13, 2017

You can specify one of the built in providers;

services.AddIdentity<User, Role>(options =>{
        options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
    })
    .AddDefaultTokenProviders();

Or create your own IUserTwoFactorTokenProvider and register it like so;

services.AddIdentity<User, Role>(options => {
    options.Tokens.PasswordResetTokenProvider = nameof(MyTokenProvider);
})
.AddTokenProvider<MyTokenProvider>(nameof(MyTokenProvider));