From 822a5e36545ed07df179ee168b063f803c8b4cef Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sun, 27 Dec 2020 10:42:22 +0800 Subject: [PATCH] Need to change the options before using IdentityOptions --- .../LINGYUN/Abp/Account/AccountAppService.cs | 10 ++++++++-- .../LINGYUN/Abp/Identity/IdentityUserAppService.cs | 8 +++++++- .../LINGYUN/Abp/Identity/MyProfileAppService.cs | 7 ++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs b/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs index 3b20014cd..dd83d33c1 100644 --- a/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs +++ b/aspnet-core/modules/account/LINGYUN.Abp.Account.Application/LINGYUN/Abp/Account/AccountAppService.cs @@ -30,8 +30,8 @@ namespace LINGYUN.Abp.Account protected IIdentityUserRepository UserRepository { get; } protected IUserSecurityCodeSender SecurityCodeSender { get; } protected IWeChatOpenIdFinder WeChatOpenIdFinder { get; } + protected IOptions IdentityOptions { get; } protected AbpWeChatMiniProgramOptionsFactory MiniProgramOptionsFactory { get; } - protected IDistributedCache SecurityTokenCache { get; } public AccountAppService( @@ -42,7 +42,8 @@ namespace LINGYUN.Abp.Account IIdentityUserRepository userRepository, IUserSecurityCodeSender securityCodeSender, IDistributedCache securityTokenCache, - AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory) + AbpWeChatMiniProgramOptionsFactory miniProgramOptionsFactory, + IOptions identityOptions) { TotpService = totpService; UserStore = userStore; @@ -52,6 +53,7 @@ namespace LINGYUN.Abp.Account SecurityCodeSender = securityCodeSender; SecurityTokenCache = securityTokenCache; MiniProgramOptionsFactory = miniProgramOptionsFactory; + IdentityOptions = identityOptions; LocalizationResource = typeof(AccountResource); } @@ -59,7 +61,9 @@ namespace LINGYUN.Abp.Account public virtual async Task RegisterAsync(WeChatRegisterDto input) { ThowIfInvalidEmailAddress(input.EmailAddress); + await CheckSelfRegistrationAsync(); + await IdentityOptions.SetAsync(); var options = await MiniProgramOptionsFactory.CreateAsync(); @@ -130,6 +134,7 @@ namespace LINGYUN.Abp.Account public virtual async Task RegisterAsync(PhoneRegisterDto input) { await CheckSelfRegistrationAsync(); + await IdentityOptions.SetAsync(); await CheckNewUserPhoneNumberNotBeUsedAsync(input.PhoneNumber); var securityTokenCacheKey = SmsSecurityTokenCacheItem.CalculateCacheKey(input.PhoneNumber, "SmsVerifyCode"); @@ -223,6 +228,7 @@ namespace LINGYUN.Abp.Account { throw new UserFriendlyException(L["InvalidSmsVerifyCode"]); } + await IdentityOptions.SetAsync(); // 传递 isConfirmed 用户必须是已确认过手机号的 var user = await GetUserByPhoneNumberAsync(input.PhoneNumber, isConfirmed: true); // 验证二次认证码 diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs index 6554b7577..1883767d7 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/IdentityUserAppService.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -15,10 +16,13 @@ namespace LINGYUN.Abp.Identity public class IdentityUserAppService : IdentityAppServiceBase, IIdentityUserAppService { protected IdentityUserManager UserManager { get; } + protected IOptions IdentityOptions { get; } public IdentityUserAppService( - IdentityUserManager userManager) + IdentityUserManager userManager, + IOptions identityOptions) { UserManager = userManager; + IdentityOptions = identityOptions; } #region OrganizationUnit @@ -106,6 +110,7 @@ namespace LINGYUN.Abp.Identity [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)] public virtual async Task ChangePasswordAsync(Guid id, ChangePasswordInput input) { + await IdentityOptions.SetAsync(); var user = await UserManager.GetByIdAsync(id); if (user.IsExternal) @@ -126,6 +131,7 @@ namespace LINGYUN.Abp.Identity [Authorize(Volo.Abp.Identity.IdentityPermissions.Users.Update)] public virtual async Task ChangeTwoFactorEnabledAsync(Guid id, ChangeTwoFactorEnabledDto input) { + await IdentityOptions.SetAsync(); var user = await UserManager.GetByIdAsync(id); (await UserManager.SetTwoFactorEnabledWithAccountConfirmedAsync(user, input.Enabled)).CheckErrors(); diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs index bec4afa46..27552962b 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Application/LINGYUN/Abp/Identity/MyProfileAppService.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Options; using System; using System.Threading.Tasks; using Volo.Abp; @@ -19,15 +20,18 @@ namespace LINGYUN.Abp.Identity protected IUserSecurityCodeSender SecurityCodeSender { get; } protected IdentityUserManager UserManager { get; } protected IIdentityUserRepository UserRepository { get; } + protected IOptions IdentityOptions { get; } public MyProfileAppService( IdentityUserManager userManager, IIdentityUserRepository userRepository, IUserSecurityCodeSender securityCodeSender, + IOptions identityOptions, IDistributedCache securityTokenCache) { UserManager = userManager; UserRepository = userRepository; + IdentityOptions = identityOptions; SecurityCodeSender = securityCodeSender; SecurityTokenCache = securityTokenCache; } @@ -38,7 +42,7 @@ namespace LINGYUN.Abp.Identity { throw new BusinessException(Volo.Abp.Identity.IdentityErrorCodes.CanNotChangeTwoFactor); } - + await IdentityOptions.SetAsync(); var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); (await UserManager.SetTwoFactorEnabledWithAccountConfirmedAsync(user, input.Enabled)).CheckErrors(); @@ -83,6 +87,7 @@ namespace LINGYUN.Abp.Identity { throw new BusinessException(IdentityErrorCodes.DuplicatePhoneNumber); } + await IdentityOptions.SetAsync(); //TODO: 可以查询缓存用 securityTokenCacheItem.SecurityToken 与 user.SecurityStamp 作对比 var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); // 更换手机号