How can I check if a password reset token is expired?

后端 未结 2 1716
故里飘歌
故里飘歌 2020-12-18 00:13

I\'m using ASP.NET Identity, and I have the basic Forgot Password/Reset Password functionality in place.

When you fill out the form that you forgot your password, it

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 00:39

    If you check the UserManager.ResetPasswordAsync(...) method, tracing throug to the VerifyUserTokenAsync method, which simply does:

    // Make sure the token is valid
    var result = await _tokenProviders[tokenProvider].ValidateAsync(purpose, token, this, user);
    

    You can just do this yourself as well, knowing that:

    • You can ask the DI Framework (e.g. via your controller constructor) for the token provider for your situation;
    • The purpose is just the hardcoded "ResetPassword" string;
    • The token is the code the user is using;
    • The user you should be able to get depending on how your view, e-mail, url, and whatever is set up (the default examples don't cover this I think, but you can easily put the user.Id in the "forgot password url" before the token itself, and extract it when you need it).

    Then you can just call ValidateAsync yourself and adjust the view accordingly.

提交回复
热议问题