Search code examples
asp.net-mvcasp.net-identityasp.net-identity-2

Manually validating a password reset token in ASP.NET Identity


I would like to manually validate a password reset token in ASP.NET Identity 2.0. I'm trying to create my own version of UserManager.ResetPasswordAsync(string userId, string token, string newPassword) that takes and IdentityUser instead of userId like this:

UserManager.ResetPasswordAsync(IdentityUser user, string token, string newPassword)

Not sure if I am doing this right, but here I am attempting to validate the code that was emailed to the user in an earlier step. I have not modified the code/token that sends the email to the user and generates the code. I am assuming this is the correct method to call, but the purpose argument is incorrect. (I tried passing "ASP.NET Identity" but no dice.)

if (await userManager.UserTokenProvider.ValidateAsync(purpose: "?", token: code, manager: userManager, user: user))
{
    return IdentityResult.Success;
}
else
{
    return new IdentityResult("Invalid code.");
}

If someone could fill me in on the details of how it works out of the box, or point me at Microsoft's source code for UserManager.ResetPasswordAsync(IdentityUser user, string token, string newPassword) that would be most appreciated!


Solution

  • It appears that the code for Microsoft.AspNet.Identity has not been Open Sourced according to the Codeplex repository located at:

    https://aspnetidentity.codeplex.com/SourceControl/latest#Readme.markdown

    At present, the ASP.NET Identity framework code is not public and therefore will not be published on this site. However, we are planning to change that, and as soon as we are able, the code will be published in this repository.

    However I did find this which might be the source for the UserManager based on the debug symbols:

    UserManager Source Code

    I also found these posts which might help:

    Implementing custom password policy using ASP.NET Identity

    UserManager Class Documentation

    IUserTokenProvider Interface Documentation