简体   繁体   中英

ASP.NET Identity - can't register UserTokenProvider

I have an issue with generating the reset password token in the ASP.NET Core 1.0 project:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddIdentity<UserDB, GroupDB>()
            .AddDefaultTokenProviders()
}

then in my controller with DI:

...
UserManager<UserDB> userManager
...


string token = await userManager.GeneratePasswordResetTokenAsync(user);

and I get

No IUserTokenProvider named 'Default' is registered.

How to fix it ?

I can see there's no Providers inside of my UserStore constructor:

在此输入图像描述

I noticed that the UserStore class didn't have the IUserTokenProvider interface implementation. Also, in the ApplicationUserManager class constructor, I check if the TokenProviderDescriptor is registered

public class ApplicationUserManager : UserManager<UserDB>
{
    public ApplicationUserManager(UserStore store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<UserDB> passwordHasher, IEnumerable<IUserValidator<UserDB>> userValidators, IEnumerable<IPasswordValidator<UserDB>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<UserDB>> logger, IHttpContextAccessor contextAccessor) 
        : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger, contextAccessor)
    {
        if (!optionsAccessor.Value.Tokens.ProviderMap.ContainsKey("DefaultTokenProvider"))
            optionsAccessor.Value.Tokens.ProviderMap.Add("DefaultTokenProvider", new TokenProviderDescriptor(typeof(UserStore)));
    }
}

That's solved the issue. Thanks for the help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM