没有IUserTokenProvider注册
我最近更新了我的申请表1.0到2.0的Asp.Net Identity Core
。
有一些我想尝试的新function,比如GenerateEmailConfirmationToken
等
我正在使用这个项目作为参考。
当用户尝试注册时,在注册的Post方法的执行过程中出现错误
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); }
在线
var code = _userManager.GenerateEmailConfirmationToken(user.Id);
我得到错误说:
No IUserTokenProvider is registered.
现在,我只想看看它产生了什么样的代码。 是否需要对inheritance自IdentityUser
类的ApplicationUser
类进行一些更改?
还是有什么我需要改变,让这些function的工作?
您必须指定一个UserTokenProvider
来生成一个令牌。
using Microsoft.Owin.Security.DataProtection; using Microsoft.AspNet.Identity.Owin; // ... var provider = new DpapiDataProtectionProvider("Sample"); var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>()); userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>( provider.Create("EmailConfirmation"));
您还应该阅读这篇文章: 使用ASP.NET标识向应用程序添加双因素身份validation 。
除了接受的答案之外,我想补充一点,这种方法在Azure网站中不起作用,您将得到CryptographicException而不是令牌。
为了使它适用于Azure,请实现您自己的IDataProtector: 查看此答案
在博客文章稍微更详细
我的解决scheme
var provider = new DpapiDataProtectionProvider("YourAppName"); UserManager.UserTokenProvider = new DataProtectorTokenProvider<User, string>(provider.Create("UserToken")) as IUserTokenProvider<User, string>;
我解决了这个代码的问题。
祝你好运的朋友。
在ASP.NET Core中,现在可以在Startup.cs中configuration一个默认服务,如下所示:
services.AddIdentity<ApplicationUser, IdentityRole>() .AddDefaultTokenProviders();
没有必要调用DpapiDataProtectionProvider
或类似的东西。 DefaultTokenProviders()将负责从UserManager调用GenerateEmailConfirmationToken。
按照上面的pisker
在startup.cs中你可以使用
services.AddIdentity<ApplicationUser, IdentityRole>() .AddDefaultTokenProviders();
在.net核心2.0中,您可能需要添加到startup.cs中:
using Microsoft.AspNetCore.Identity;
这对我来说工作得很好。
如果其他人犯了同样的错误,我做了。 我试图做一个像下面的函数,并确定错误“没有IUserTokenProvider注册”。 失去了。 然而,只要我试图使用从“callbackUrl”生成的链接,我得到了错误“无效的令牌。 为了使其工作,您需要获取HttpContext UserManager。 如果你使用一个标准的ASP.NET MVC 5应用程序和个人用户帐户,你可以像我的例子二那样做。
首先尝试不起作用:
public ActionResult Index() { //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(); }
代码工作:
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(); }
你可能也有兴趣在这里寻找:
如何在ASP.NET身份1.1 nightly构build中实现TokenProvider?
使用Microsoft.Identity.Owin包中的默认令牌提供程序实现
更新身份后,我得到了同样的错误,并发现这是因为我使用的是Unity。
在解决scheme上看到这个问题线程: 用Unity注册IAuthenticationManager
下面也是快速参考:
下面是我为了让Unity在ASP.NET Identity 2.0中发挥出色所做的一切:
我将以下内容添加到
UnityConfig
类的RegisterTypes
方法中: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());