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

I am trying to implement the password reset functionality with an ASP.NET Identity 1.1 nightly build. There is a UserManager.GetPasswordResetToken method, but it throws an exception "No ITokenProvider registered." Is there a built-in token provider in ASP.NET Identity? If so, how can I register it? If not, how can I implement it? Will be used by default token provider in 1.1. release? And the last question: is there a release date for 1.1?

+7
c # asp.net-mvc-5 asp.net-identity
Oct 23 '13 at 10:48 on
source share
4 answers

The default token provider implementation is in the Microsoft.Identity.Owin package:

 /// <summary> /// Token provider that uses a DataProtector to generate encrypted tokens /// </summary> public class DataProtectorTokenProvider : ITokenProvider { public DataProtectorTokenProvider(IDataProtector protector) 

And you will do something like this to connect it using the default data protection provider from your OWIN IAppBuilder

 IDataProtectionProvider provider = app.GetDataProtectionProvider(); if (provider != null) { manager.PasswordResetTokens = new DataProtectorTokenProvider(provider.Create("PasswordReset")); manager.UserConfirmationTokens = new DataProtectorTokenProvider(provider.Create("ConfirmUser")); } 
+6
Oct 23 '13 at 18:04 on
source share

If someone is looking for a solution in AspNet.Identity 2.0 beta1.

Only this needs to be changed.

 UserManager.UserTokenProvider = new DataProtectorTokenProvider <SecurityUser, string>(provider.Create("UserToken")) as IUserTokenProvider<SecurityUser, string>; 

PasswordResetTokens and UserConfirmationTokens are combined in the UserTokenProvider property, and the token provider class is also changed.

+19
Feb 14 '14 at 10:16
source share

Another way to do this (based on other answers, but simplifying it a bit) is to modify Startup.Auth.cs so that it looks something like this:

 public partial class Startup { internal static IDataProtectionProvider DataProtectionProvider { get; private set; } public void ConfigureAuth(IAppBuilder app) { DataProtectionProvider = app.GetDataProtectionProvider(); } } 

Then change the default constructor in AccountController.cs so that it looks something like this:

  public AccountController() : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) { if (Startup.DataProtectionProvider != null) { this.UserManager.PasswordResetTokens = new DataProtectorTokenProvider(Startup.DataProtectionProvider.Create("PasswordReset")); this.UserManager.UserConfirmationTokens = new DataProtectorTokenProvider(Startup.DataProtectionProvider.Create("ConfirmUser")); } } 
+5
Oct 26 '13 at 18:09
source share

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

 public partial class Startup { static Startup() { UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>()); } public static Func<UserManager<IdentityUser>> 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 run the UserManager in AccountController using this UserManagerFactory

 public AccountController() : this(Startup.UserManagerFactory()) { } public AccountController(UserManager<IdentityUser> userManager) { UserManager = userManager; } public UserManager<IdentityUser> UserManager { get; private set; } 
+1
Oct 24 '13 at 13:35
source share



All Articles