11

I'm looking to send the user an SMS when reseting their password. I already have the facilities to send a SMS, I just need a guide on how to set it up with Identity 2.0. I can't seem to find any useful info online, the reference code itself isn't properly commented either.

I want to generate a security code, send it to the user, he must then input it into a form and then be allowed to reset his/her password. Can anyone direct me to a guide/tutorial that explains this process?

4

1 回答 1

5

在挖掘身份源代码后,我找到了一个替代令牌提供程序,它可以生成类似于电话号码确认(六位数)的令牌。

我必须在我的 UserManager 中实现两种方法来生成代码,然后对其进行验证。

我在 UserManager 中声明了令牌提供程序

private TotpSecurityStampBasedTokenProvider<User, string> smsResetTokenProvider = new TotpSecurityStampBasedTokenProvider<User, string>();

这是生成代码的第一种方法:

public async Task<string> GenerateSMSPasswordResetToken(string userId)
    {
        var user = await base.FindByIdAsync(userId);
        var token = await smsResetTokenProvider.GenerateAsync("Reset Password", this, user);
        return token;
    }

这是验证代码的第二种方法:

public async Task<IdentityResult> SMSPasswordResetAsync(string userId, string token, string newPassword)
    {
        var user = await base.FindByIdAsync(userId);
        var valid = await smsResetTokenProvider.ValidateAsync("Reset Password", token, this, user);
        if (valid)
        {
            var passwordStore = Store as IUserPasswordStore<User, string>;

            var result = await UpdatePassword(passwordStore, user, newPassword);
            if (!result.Succeeded)
            {
                return result;
            }
            return await UpdateAsync(user);
        }
        else
        {
            return IdentityResult.Failed("InvalidToken");
        }
    }

您可能需要根据您的用户管理器调整代码

于 2015-12-16T09:51:26.910 回答