【发布时间】:2015-02-21 10:45:45
【问题描述】:
我正在编写 MVC 5 并使用 Identity 2.0。
现在我正在尝试重置密码。但我总是收到重置密码令牌的“无效令牌”错误。
public class AccountController : Controller
{
public UserManager<ApplicationUser> UserManager { get; private set; }
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
我设置了 DataProtectorTokenProvider,
public AccountController(UserManager<ApplicationUser> userManager)
{
//usermanager config
userManager.PasswordValidator = new PasswordValidator { RequiredLength = 5 };
userManager.EmailService = new IddaaWebSite.Controllers.MemberShip.MemberShipComponents.EmailService();
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider();
userManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("UserToken"))
as IUserTokenProvider<ApplicationUser, string>;
UserManager = userManager;
}
我在发送邮件之前生成密码重置
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ManagePassword(ManageUserViewModel model)
{
if (Request.Form["email"] != null)
{
var email = Request.Form["email"].ToString();
var user = UserManager.FindByEmail(email);
var token = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
//mail send
}
}
我点击邮件中的链接,我正在获取密码重置令牌并使用
var result = await UserManager.ResetPasswordAsync(model.UserId, model.PasswordToken, model.NewPassword);
结果总是假的,它说“无效令牌”。 我应该在哪里解决?
【问题讨论】:
-
您将生成的令牌存储在哪里?
-
@vgSefa 在 Identity 中,令牌已签名,这意味着无需将它们存储在某处。 二看:是否生成了token?我认为在设置 UserTokenProvider 时不需要强制转换。也许那里出了点问题。
-
@Horizon_Net 是的,它生成并尝试移除铸件。还是不行
-
只是另一个想法:相应用户的电子邮件地址是否已经确认?如here 所述:如果未确认用户电子邮件,该方法会静默失败。如果针对无效电子邮件地址发布错误,恶意用户可以使用该信息找到有效的 userId(电子邮件别名)进行攻击。
-
好的,谢谢。我添加了电子邮件确认条件。
标签: c# .net asp.net-identity