How to implement a TokenProvider in ASP.NET Identity 1.1 nightly build?

后端 未结 4 1305
北海茫月
北海茫月 2021-02-06 06:49

I\'m trying to implement password reset functionality with nightly build of ASP.NET Identity 1.1. There is a UserManager.GetPasswordResetToken method, but it throws an exception

4条回答
  •  春和景丽
    2021-02-06 06:58

    Ok, answering my own question based on @hao-kung reply. First add static constructor and UserManagerFactory to Statrup class (startup.auth.cs)

    public partial class Startup
    {
        static Startup()
        {
            UserManagerFactory = () => new UserManager(new UserStore());
        }
    
        public static Func> UserManagerFactory { get; set; }
    
        public void ConfigureAuth(IAppBuilder app)
        {
            var manager = UserManagerFactory();
            IDataProtectionProvider provider = app.GetDataProtectionProvider();
            if (provider != null)
            {
                manager.PasswordResetTokens = new DataProtectorTokenProvider(provider.Create("PasswordReset"));
                manager.UserConfirmationTokens = new DataProtectorTokenProvider(provider.Create("ConfirmUser"));
            }
    
    
            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        }
    }
    

    Then init UserManager in the AccountController using that UserManagerFactory

    public AccountController() : this(Startup.UserManagerFactory())
    {
    }
    
    public AccountController(UserManager userManager)
    {
        UserManager = userManager;
    }
    
    public UserManager UserManager { get; private set; }
    

提交回复
热议问题