我最近将我的应用程序的Asp.Net Identity Core
从1.0更新到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
类进行一些更改吗?
或者我需要做些改变才能让这些函数正常工作?
发布于 2014-03-25 21:03:33
您必须指定UserTokenProvider
才能生成令牌。
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。
发布于 2017-02-23 23:33:53
在ASP.NET核心中,现在可以在Startup.cs中配置默认服务,如下所示:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders();
不需要调用DpapiDataProtectionProvider
或类似的东西。DefaultTokenProviders()将负责从UserManager调用GenerateEmailConfirmationToken。
发布于 2014-06-21 08:19:13
除了公认的答案之外,我还想补充说,这种方法在Azure网站上是行不通的,你会得到CryptographicException而不是token。
要修复Azure,请实现您自己的IDataProtector:see this answer
blog-post中的更多细节
https://stackoverflow.com/questions/22629936
复制相似问题