No IUserTokenProvider is registered

I recently updated Asp.Net Identity Core my application form 1.0 to 2.0.

There are new features that I wanted to try, for example, GenerateEmailConfirmationToken , etc.

I am using this project as a link.

When a user tries to register, I get an error when executing the Post method for Register

 private readonly UserManager<ApplicationUser> _userManager; public ActionResult Register(RegisterViewModel model) { if (ModelState.IsValid) { var ifUserEXists = _userManager.FindByName(model.EmailId); if (ifUserEXists == null) return View(model); var confirmationToken = _userRepository.CreateConfirmationToken();//this is how i'm generating token currently. var result = _userRepository.CreateUser(model,confirmationToken); var user = _userManager.FindByName(model.EmailId); if (result) { var code = _userManager.GenerateEmailConfirmationToken(user.Id);//error here _userRepository.SendEmailConfirmation(model.EmailId, model.FirstName, confirmationToken); //Information("An email is sent to your email address. Please follow the link to activate your account."); return View("~/Views/Account/Thank_You_For_Registering.cshtml"); } } //Error("Sorry! email address already exists. Please try again with different email id."); ModelState.AddModelError(string.Empty, Resource.AccountController_Register_Sorry__User_already_exists__please_try_again_); return View(model); } 

In line

  var code = _userManager.GenerateEmailConfirmationToken(user.Id); 

I get an error message:

 No IUserTokenProvider is registered. 

So far, I just wanted to see what code it generates. Do I need to make some changes to my ApplicationUser class, which inherits from the IdentityUser class?

Or is there something I need to change to make this function work?

+94
c # asp.net-identity-2
Mar 25 '14 at 9:14
source share
10 answers

You must specify a UserTokenProvider to generate a token.

 using Microsoft.Owin.Security.DataProtection; using Microsoft.AspNet.Identity.Owin; // ... var provider = new DpapiDataProtectionProvider("SampleAppName"); var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>()); userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>( provider.Create("SampleTokenName")); 

You should also read this article: Adding Two-Factor Authentication to an Application Using an ASP.NET Identity .

+120
Mar 25 '14 at 13:03
source share

In addition to the accepted answer, I would like to add that this approach will not work on Azure websites: instead of a token, you will get a CryptographicException.

To fix this in Azure, implement your own IDataProtector: see this answer

A bit more in the blog

+20
Jun 21 '14 at 0:19
source share

In ASP.NET Core, you can now configure the default service in Startup.cs as follows:

 services.AddIdentity<ApplicationUser, IdentityRole>() .AddDefaultTokenProviders(); 

There is no need to call DpapiDataProtectionProvider or something like that. DefaultTokenProviders () will take care of calling the GenerateEmailConfirmationToken from the UserManager.

+20
Feb 23 '17 at 15:33
source share

My decision:

  var provider = new DpapiDataProtectionProvider("YourAppName"); UserManager.UserTokenProvider = new DataProtectorTokenProvider<User, string>(provider.Create("UserToken")) as IUserTokenProvider<User, string>; 

My problem with this code has been resolved.
Friends of good luck.

+11
Nov 12 '14 at 1:42
source share

If someone else makes the same mistake as me. I tried to make a function similar to the one below and I'm pretty sure that the error is “No IUserTokenProvider”. gone. However, as soon as I tried to use the link created from "callbackUrl", I got the error "Invalid token." For it to work, you need to get the HttpContext UserManager. If you are using the standard ASP.NET MVC 5 application with separate user accounts, you can do this as shown below.

Code that works:

 public ActionResult Index() { //Code to create ResetPassword URL var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = userManager.FindByName("useremail@gmail.com"); string code = userManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); return View(); } 

First try what doesn't work, No IUserTokenProvider is registered. disappeared, but the generated URL gets an Invalid token. .

 public ActionResult NotWorkingCode() { //DOES NOT WORK - When used the error "Invalid token." will always trigger. var provider = new DpapiDataProtectionProvider("ApplicationName"); var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext())); userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("ASP.NET Identity")); var user = userManager.FindByName("useremail@gmail.com"); string code = userManager.GeneratePasswordResetToken(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //DOES NOT WORK - When used the error "Invalid token." will always trigger. return View(); } 
+7
Dec 29 '15 at 16:59
source share

According to pisker above

In startup.cs you can use

 services.AddIdentity<ApplicationUser, IdentityRole>() .AddDefaultTokenProviders(); 

In .net core 2.0, you may need to add to startup.cs:

 using Microsoft.AspNetCore.Identity; 

It worked for me.

+5
Aug 22 '17 at 14:48
source share

You may also be interested in here:
How to implement TokenProvider in ASP.NET Identity 1.1 nightly build?
to use the default token provider implementation from the Microsoft.Identity.Owin package

0
Dec 11 '14 at 9:31
source share

I got the same error after updating Identity and found it was because I used Unity.

See this topic for a solution: Register IAuthenticationManager with Unity

Also below for quick reference:

Here is what I did to make Unity play well with ASP.NET Identity 2.0:

I added the following to the RegisterTypes method in the UnityConfig class:

 container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager()); container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager()); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager()); container.RegisterType<AccountController>(new InjectionConstructor()); 
0
03 Feb '15 at 17:32
source share

in IdentityConfig.cs, add your two-factor version:

  manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser> { MessageFormat = "Your security code is {0}" }); manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationUser> { Subject = "Security Code", BodyFormat = "Your security code is {0}" }); //config sms service manager.SmsService = new Services.SMS(); var dataProtectionProvider = options.DataProtectionProvider; if (dataProtectionProvider != null) { manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); } 
0
Jul 28 '18 at 10:17
source share

When modifying .NET Core Identity files, I came across this error:

NotSupportedException: IUserTwoFactorTokenProvider with the name "Default" is not registered.

Microsoft.AspNetCore.Identity.UserManager.GenerateUserTokenAsync

The function could not generate a token, because no provider was available when registering a new user. This mistake is easy to fix!

If you are like me, you changed the AddDefaultIdentity in the Startup.cs file in AddIdentity . I wanted to implement my own user inherited from the base user. By doing this, I have lost my default token providers. The fix was to add them back using AddDefaultTokenProviders() .

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

After this fix everything worked again!

source: https://mattferderer.com/NotSupportedException-No-IUserTwoFactorTokenProvider-tuser-named-default-registered

0
Jul 13 '19 at 8:31
source share



All Articles