我最近将我的申请表 1.0 的 Asp.Net Identity Core
更新到了 2.0。
有一些我想尝试的新功能,比如 GenerateEmailConfirmationToken
等
我正在使用 this项目作为引用。
当用户尝试注册时,我在执行 Register 的 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.
现在,我只想看看它生成什么样的代码。
我是否需要对继承自 IdentityUser
类的 ApplicationUser
类进行一些更改?
或者是否需要更改某些内容才能使这些功能正常工作?
最佳答案
您必须指定 UserTokenProvider
才能生成 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"));
您还应该阅读这篇文章:Adding Two Factor Authentication to an Application Using ASP.NET Identity .
关于c# - 没有注册 IUserTokenProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629936/