当我试图重置密码时,我使用userManager.VerifyUserTokenAsync(user, tokenProvider, purpose, token)
,但它总是返回false。我的怀疑是purpose
字段是错误的,但我不确定实际应该去哪里。我的问题是,如何获得purpose
字段?从我看到的一些例子中,人们将其设置为"重置密码"。这是正确的还是我遗漏了什么?
谢谢!
启动.cs
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedAccount = true;
options.SignIn.RequireConfirmedEmail = true;
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
options.Tokens.EmailConfirmationTokenProvider = "emailconfirmation";
})
.AddEntityFrameworkStores<VisualDbContext>()
.AddDefaultTokenProviders()
.AddTokenProvider<EmailConfirmationTokenProvider<ApplicationUser>>("emailconfirmation");
services.Configure<DataProtectionTokenProviderOptions>(opt =>
opt.TokenLifespan = TimeSpan.FromHours(2));
services.Configure<EmailConfirmationTokenProviderOptions>(opt =>
opt.TokenLifespan = TimeSpan.FromDays(3));
// Forgot Password
var token = await _userManager.GeneratePasswordResetTokenAsync(user);
var encodedToken = HttpUtility.UrlEncode(token);
//Verify Reset Token
var purpose = "WHAT GOES HERE?";
var result = await _userManager.VerifyUserTokenAsync(user, TokenOptions.DefaultEmailProvider, purpose, token);
感谢
;象征性目的";为在UserManager类中使用而定义的常量,如您在UserManager源代码中所见。
在您的特定用例中,您希望这样做:
//Replace TUser with your user type
var purpose = UserManager<TUser>.ResetPasswordTokenPurpose;
var result = await _userManager.VerifyUserTokenAsync(user, TokenOptions.DefaultEmailProvider, purpose, token);
或者你可以这样做:
var purpose = UserManager<object>.ResetPasswordTokenPurpose;
不幸的是,因为UserManager类是泛型的,所以必须编写一些难看的代码才能访问常量。也许在未来,微软可以像这里描述的那样添加一个非泛型父类,以使访问目的常量更加优雅。