在ASP.NET标识中手动验证密码重置标记

Jer*_*ook 8 asp.net-mvc asp.net-identity asp.net-identity-2

我想在ASP.NET Identity 2.0中手动验证密码重置令牌.我正在尝试创建自己的版本UserManager.ResetPasswordAsync(string userId, string token, string newPassword),而IdentityUser不是userId像这样:

UserManager.ResetPasswordAsync(IdentityUser user, string token, string newPassword)
Run Code Online (Sandbox Code Playgroud)

不确定我是否正确这样做,但在这里我试图验证在早期步骤中通过电子邮件发送给用户的代码.我没有修改将电子邮件发送给用户并生成代码的代码/令牌.我假设这是正确的调用方法,但purpose参数不正确.(我尝试传递"ASP.NET身份",但没有骰子.)

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

如果有人可以填写我开箱即用的详细信息,或者指出我的微软源代码,UserManager.ResetPasswordAsync(IdentityUser user, string token, string newPassword)那将是非常感谢!

Jer*_*ook 8

我通过将目的设置为"ResetPassword"克服了我的问题.

下面是最终结果的片段,以防有人想要做类似的事情.这是我ApplicationUserManager班上的一种方法.但要意识到,Microsoft实现的某些异常处理缺失或未本地化,因为其代码中使用的某些私有变量,方法和资源是不可访问的.不幸的是,他们没有保护这些东西,所以我可以得到它.缺少的ThrowIfDisposed方法调用对我来说特别有趣(和集市).显然,他们在实例处理后预期方法调用,以便提供更友好的错误消息并避免意外.

public async Task<IdentityResult> ResetPasswordAsync(IdentityUser user,
    string token, string newPassword)
{
    if (user == null)
    {
        throw new ArgumentNullException("user");
    }

    // Make sure the token is valid and the stamp matches.
    if (!await UserTokenProvider.ValidateAsync("ResetPassword", token, 
        this, user))
    {
        return IdentityResult.Failed("Invalid token.");
    }

    // Make sure the new password is valid.
    var result = await PasswordValidator.ValidateAsync(newPassword)
        .ConfigureAwait(false);
    if (!result.Succeeded)
    {
        return result;
    }

    // Update the password hash and invalidate the current security stamp.
    user.PasswordHash = PasswordHasher.HashPassword(newPassword);
    user.SecurityStamp = Guid.NewGuid().ToString();

    // Save the user and return the outcome.
    return await UpdateAsync(user).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*ion 3

根据位于以下位置的 Codeplex 存储库,Microsoft.AspNet.Identity 的代码似乎尚未开源:

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

目前,ASP.NET Identity 框架代码尚未公开,因此不会在本网站上发布。但是,我们计划改变这一点,一旦有能力,代码将在此存储库中发布。

但是我确实发现这可能是基于调试符号的 UserManager 的来源:

用户管理器源代码

我还发现这些帖子可能会有所帮助:

使用 ASP.NET Identity 实施自定义密码策略

UserManager 类文档

IUserTokenProvider接口文档