我正在使用Asp.Net-Identity-2,我正在尝试使用以下方法验证电子邮件验证码.但我收到"无效令牌"错误消息.
我的应用程序的用户管理器是这样的:
public class AppUserManager : UserManager<AppUser>
{
public AppUserManager(IUserStore<AppUser> store) : base(store) { }
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));
manager.PasswordValidator = new PasswordValidator {
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true
};
manager.UserValidator = new UserValidator<AppUser>(manager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = true
};
var dataProtectionProvider = options.DataProtectionProvider;
//token life span is 3 hours …Run Code Online (Sandbox Code Playgroud)这是与确认电子邮件中的此aspnet身份无效令牌非常相似的问题, 但解决方案无效,因为我使用的是包含ASP.NET核心身份的新ASP.NET Core 1.0.
我的方案如下:
在后端(ASP.NET核心)我有一个功能,发送密码重置电子邮件与链接.为了生成该链接,我必须使用Identity生成代码.像这样的东西.
public async Task SendPasswordResetEmailAsync(string email)
{
//_userManager is an instance of UserManager<User>
var userEntity = await _userManager.FindByNameAsync(email);
var tokenGenerated = await _userManager.GeneratePasswordResetTokenAsync(userEntity);
var link = Url.Action("MyAction", "MyController", new { email = email, code = tokenGenerated }, protocol: HttpContext.Request.Scheme);
//this is my service that sends an email to the user containing the generated password reset link
await _emailService.SendPasswordResetEmailAsync(userEntity , link);
}
Run Code Online (Sandbox Code Playgroud)
这将生成一个电子邮件,其中包含以下链接:
http://myapp:8080/passwordreset?code=CfDJ8JBnWaVj6h1PtqlmlJaH57r9TRA5j7Ij1BVyeBUpqX+5Cq1msu9zgkuI32Iz9x/5uE1B9fKFp4tZFFy6lBTseDFTHSJxwtGu+jHX5cajptUBiVqIChiwoTODh7ei4+MOkX7rdNVBMhG4jOZWqqtZ5J30gXr/JmltbYxqOp4JLs8V05BeKDbbVO/Fsq5+jebokKkR5HEJU+mQ5MLvNURsJKRBbI3qIllj1RByXt9mufGRE3wmQf2fgKBkAL6VsNgB8w==
然后,我的AngularJs应用程序将呈现一个视图,其中包含一个用于输入和确认新密码的表单,并将使用新密码和从URL中的查询参数获取的代码输出JSON对象.
最后,我的后端将获得PUT请求,获取代码并使用Identity验证它,如下所示:
[HttpPut]
[AllowAnonymous]
[Route("api/password/{email}")]
public async Task<IActionResult> …Run Code Online (Sandbox Code Playgroud)当用户在输入新密码后尝试在重置密码屏幕上重置密码时,我们会收到无效令牌错误消息。通常这对每个人都很好,即使有一些特殊的字符,比如#。我们现在有一个案例,某人在重置密码屏幕上将 * 输入到他的新密码中,只是因为这个特殊字符而收到此错误消息。
我现在已经尝试了数小时的研究来找到为什么会发生这种情况的解决方案,但没有运气。我在这里找到了这个解决方案,它在用户名中存在特殊字符的问题,但我们没有这个问题。只有密码中的那个特殊字符有问题。由于我们已经在生产中,我们不能只禁止在密码中使用该字符。
有人有线索吗?
生成令牌控制器方法:
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Email.ToLower());
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user.UserName)))
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
var code = await _userManager.GeneratePasswordResetTokenAsync(user.UserName);
code = …Run Code Online (Sandbox Code Playgroud)