Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed reset password for code #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using EasyAbp.Abp.PhoneNumberLogin.Account.Dtos;
using EasyAbp.Abp.PhoneNumberLogin.Identity;
using EasyAbp.Abp.PhoneNumberLogin.Localization;
using EasyAbp.Abp.PhoneNumberLogin.Settings;
using EasyAbp.Abp.VerificationCode;
using IdentityModel.Client;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Identity;
using Volo.Abp.Settings;
using Volo.Abp.Uow;
Expand All @@ -28,6 +33,8 @@ public class PhoneNumberLoginAccountAppService : ApplicationService, IPhoneNumbe
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;
private readonly ISettingProvider _settingProvider;
private readonly IDistributedCache<string> _distributedCache;
private readonly IStringLocalizer<PhoneNumberLoginResource> _localizer;

public PhoneNumberLoginAccountAppService(
IPhoneNumberLoginVerificationCodeSender phoneNumberLoginVerificationCodeSender,
Expand All @@ -38,7 +45,9 @@ public PhoneNumberLoginAccountAppService(
IHttpClientFactory httpClientFactory,
IConfiguration configuration,
ISettingProvider settingProvider,
IdentityUserManager identityUserManager)
IdentityUserManager identityUserManager,
IDistributedCache<string> distributedCache,
IStringLocalizer<PhoneNumberLoginResource> localizer)
{
_phoneNumberLoginVerificationCodeSender = phoneNumberLoginVerificationCodeSender;
_phoneNumberLoginNewUserCreator = phoneNumberLoginNewUserCreator;
Expand All @@ -49,6 +58,8 @@ public PhoneNumberLoginAccountAppService(
_httpClientFactory = httpClientFactory;
_settingProvider = settingProvider;
_configuration = configuration;
_distributedCache = distributedCache;
_localizer = localizer;
}

public virtual async Task<SendVerificationCodeResult> SendVerificationCodeAsync(SendVerificationCodeInput input)
Expand Down Expand Up @@ -99,11 +110,21 @@ public virtual async Task<ConfirmPhoneNumberResult> ConfirmPhoneNumberAsync(Conf

public virtual async Task ResetPasswordAsync(ResetPasswordWithPhoneNumberInput input)
{
var result = await GetValidateResultAsync(input.PhoneNumber, input.VerificationCode, VerificationCodeType.ResetPassword);
if (!result)
{
throw new InvalidVerificationCodeException();
}
await _identityOptions.SetAsync();

var identityUser = await _uniquePhoneNumberIdentityUserRepository.GetByConfirmedPhoneNumberAsync(input.PhoneNumber);

(await _identityUserManager.ResetPasswordAsync(identityUser, input.VerificationCode, input.Password)).CheckErrors();
// VerifyTwoFactor
if (!await _identityUserManager.VerifyTwoFactorTokenAsync(identityUser, TokenOptions.DefaultPhoneProvider, input.VerificationCode))
{
throw new UserFriendlyException(_localizer["InvalidVerificationCode"]);
}
var resetPwdToken = await _identityUserManager.GeneratePasswordResetTokenAsync(identityUser);
(await _identityUserManager.ResetPasswordAsync(identityUser, resetPwdToken, input.Password)).CheckErrors();
}

public virtual async Task<TryRegisterAndRequestTokenResult> TryRegisterAndRequestTokenAsync(TryRegisterAndRequestTokenInput input)
Expand Down Expand Up @@ -164,9 +185,8 @@ protected virtual async Task<bool> GetValidateResultAsync(string phoneNumber, st
{
case VerificationCodeType.ResetPassword:

// Not able to validate reset password token here using default asp.net identity implementation

return true;
var tempCode = await _distributedCache.GetAsync($"{PhoneNumberLoginConsts.VerificationCodeCachePrefix}:{type}:{phoneNumber}");
return tempCode.Equals(code);

case VerificationCodeType.Register:

Expand Down Expand Up @@ -276,7 +296,19 @@ protected virtual async Task<string> GenerateCodeAsync(string phoneNumber, Verif
switch (type)
{
case VerificationCodeType.ResetPassword:
code = await _identityUserManager.GeneratePasswordResetTokenAsync(identityUser);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use ASP.NET Core's IdentityUserManager.GeneratePasswordResetTokenAsync();? The default implementation of IUserTwoFactorTokenProvider is fine and EasyAbp also provides one: https://github.com/EasyAbp/Abp.VerificationCode/blob/master/src/EasyAbp.Abp.VerificationCode.Identity/EasyAbp/Abp/VerificationCode/Identity/AbpVerificationCodeTokenProvider.cs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeneratePasswordResetTokenAsync() this method generate code length too long , the sms not allow code longer than 24 in aliyun .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Abp.VerficationCode not implement twoFactor methods yet . I need to take time to fix that repository ,wait some days please when i have time .

if (identityUser == null)
throw new UserFriendlyException(_localizer["InvalidPhoneNumber"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about throw new InvalidVerificationCodeException();?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if u used twofactor to generate , u need use identityuser not null , identityuser come from phonenumber in database. database doesn`t have this phonenumber u cant resetpassword.

var tspan = await GetRegisterCodeCacheSecondsAsync();
code = await _distributedCache.GetOrAddAsync($"{PhoneNumberLoginConsts.VerificationCodeCachePrefix}:{type}:{phoneNumber}",
async () =>
{
return await _identityUserManager.GenerateTwoFactorTokenAsync(identityUser, TokenOptions.DefaultPhoneProvider);
},
() => new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(tspan)
}
);
break;

case VerificationCodeType.Register:
Expand Down