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

后端 未结 5 2339
时光说笑
时光说笑 2021-02-09 22:30

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 registe

相关标签:
5条回答
  • 2021-02-09 22:55

    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));
    
    0 讨论(0)
  • 2021-02-09 22:58

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

    And the optional configuration in ConfigureServices:

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

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

    PS: Opened an issue for clarification

    0 讨论(0)
  • 2021-02-09 23:10

    Add .AddDefaultTokenProviders(); in ConfigureServices() method in startupclass

     `in public void ConfigureServices(IServiceCollection services)
        {
         services.AddIdentity<RegisterUser, IdentityRole>()
                        .AddEntityFrameworkStores<ContextClass>()
                        .AddDefaultTokenProviders();
        }
        `
    
    0 讨论(0)
  • 2021-02-09 23:12

    add or correct following lines in startup.cs:

    services.AddIdentity<User, UserRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
    

    you can found more description here

    0 讨论(0)
  • 2021-02-09 23:21

    You have to dig deep into the .NET Core code and find what internals the AddIdentity is doing.

    What I found was the following that worked for us as we could not use .AddIdentity() as it overrode the IdentityServer4 middleware.

    Instead, we added transients for the all the interfaces needed for UserManager and then used IdentityBuilder class to add the token provider.

    NOTE The User class below inherits from IdentityUser as we needed to customize our user table.

    // add User Manager related objects into DI configuration
    services.AddTransient<IUserStore<User>, UserStore<User, IdentityRole<string>, ApplicationDbContext>>();
    services.AddTransient<IRoleStore<IdentityRole<string>>, RoleStore<IdentityRole<string>, ApplicationDbContext>>();
    services.AddTransient<IPasswordHasher<User>, PasswordHasher<User>>();
    services.AddTransient<ILookupNormalizer, UpperInvariantLookupNormalizer>();
    services.AddTransient<IdentityErrorDescriber>();
    var identityBuilder = new IdentityBuilder(typeof(User), typeof(IdentityRole<string>), services);
    identityBuilder.AddTokenProvider("Default", typeof(DataProtectorTokenProvider<User>));
    services.AddTransient<UserManager<User>>();
    
    0 讨论(0)
提交回复
热议问题