|
|
|
@ -2,72 +2,37 @@ |
|
|
|
using Microsoft.AspNetCore.Authorization; |
|
|
|
using Microsoft.AspNetCore.Identity; |
|
|
|
using Microsoft.Extensions.Caching.Distributed; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using System; |
|
|
|
using System.Security.Claims; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Volo.Abp; |
|
|
|
using Volo.Abp.Account.Localization; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Volo.Abp.Caching; |
|
|
|
using Volo.Abp.Identity; |
|
|
|
using Volo.Abp.Settings; |
|
|
|
using Volo.Abp.Users; |
|
|
|
|
|
|
|
namespace LINGYUN.Abp.Account |
|
|
|
{ |
|
|
|
[Authorize] |
|
|
|
public class MyProfileAppService : ApplicationService, IMyProfileAppService |
|
|
|
public class MyProfileAppService : AccountApplicationServiceBase, IMyProfileAppService |
|
|
|
{ |
|
|
|
protected IDistributedCache<SmsSecurityTokenCacheItem> SecurityTokenCache { get; } |
|
|
|
protected IUserSecurityCodeSender SecurityCodeSender { get; } |
|
|
|
protected IdentityUserManager UserManager { get; } |
|
|
|
protected Identity.IIdentityUserRepository UserRepository { get; } |
|
|
|
protected IOptions<IdentityOptions> IdentityOptions { get; } |
|
|
|
|
|
|
|
public MyProfileAppService( |
|
|
|
IdentityUserManager userManager, |
|
|
|
Identity.IIdentityUserRepository userRepository, |
|
|
|
IUserSecurityCodeSender securityCodeSender, |
|
|
|
IOptions<IdentityOptions> identityOptions, |
|
|
|
IDistributedCache<SmsSecurityTokenCacheItem> securityTokenCache) |
|
|
|
{ |
|
|
|
UserManager = userManager; |
|
|
|
UserRepository = userRepository; |
|
|
|
IdentityOptions = identityOptions; |
|
|
|
SecurityCodeSender = securityCodeSender; |
|
|
|
SecurityTokenCache = securityTokenCache; |
|
|
|
|
|
|
|
LocalizationResource = typeof(AccountResource); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task SetClaimAsync(ChangeUserClaimInput input) |
|
|
|
{ |
|
|
|
await IdentityOptions.SetAsync(); |
|
|
|
var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); |
|
|
|
|
|
|
|
var newClaim = new Claim(input.ClaimType, input.ClaimValue); |
|
|
|
var currentClaim = user.FindClaim(newClaim); |
|
|
|
if (currentClaim != null) |
|
|
|
{ |
|
|
|
// Replace With Claim Value Empty?
|
|
|
|
// (await UserManager.ReplaceClaimAsync(user, currentClaim.ToClaim(), newClaim)).CheckErrors();
|
|
|
|
user.ReplaceClaim(currentClaim.ToClaim(), newClaim); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// (await UserManager.AddClaimAsync(user, newClaim)).CheckErrors();
|
|
|
|
user.AddClaim(GuidGenerator, newClaim); |
|
|
|
} |
|
|
|
(await UserManager.UpdateAsync(user)).CheckErrors(); |
|
|
|
|
|
|
|
await CurrentUnitOfWork.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<TwoFactorEnabledDto> GetTwoFactorEnabledAsync() |
|
|
|
{ |
|
|
|
await IdentityOptions.SetAsync(); |
|
|
|
var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); |
|
|
|
var user = await GetCurrentUserAsync(); |
|
|
|
|
|
|
|
return new TwoFactorEnabledDto |
|
|
|
{ |
|
|
|
@ -83,8 +48,7 @@ namespace LINGYUN.Abp.Account |
|
|
|
// throw new BusinessException(Volo.Abp.Identity.IdentityErrorCodes.CanNotChangeTwoFactor);
|
|
|
|
//}
|
|
|
|
// TODO: Abp官方移除了双因素的设置,不排除以后会增加,如果在用户接口中启用了双因素认证,可能造成登录失败!
|
|
|
|
await IdentityOptions.SetAsync(); |
|
|
|
var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); |
|
|
|
var user = await GetCurrentUserAsync(); |
|
|
|
|
|
|
|
(await UserManager.SetTwoFactorEnabledWithAccountConfirmedAsync(user, input.Enabled)).CheckErrors(); |
|
|
|
|
|
|
|
@ -106,7 +70,8 @@ namespace LINGYUN.Abp.Account |
|
|
|
{ |
|
|
|
throw new BusinessException(Identity.IdentityErrorCodes.DuplicatePhoneNumber); |
|
|
|
} |
|
|
|
var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); |
|
|
|
var user = await GetCurrentUserAsync(); |
|
|
|
|
|
|
|
var template = await SettingProvider.GetOrNullAsync(Identity.Settings.IdentitySettingNames.User.SmsPhoneNumberConfirmed); |
|
|
|
var token = await UserManager.GenerateChangePhoneNumberTokenAsync(user, input.NewPhoneNumber); |
|
|
|
// 发送验证码
|
|
|
|
@ -128,9 +93,7 @@ namespace LINGYUN.Abp.Account |
|
|
|
{ |
|
|
|
throw new BusinessException(Identity.IdentityErrorCodes.DuplicatePhoneNumber); |
|
|
|
} |
|
|
|
await IdentityOptions.SetAsync(); |
|
|
|
//TODO: 可以查询缓存用 securityTokenCacheItem.SecurityToken 与 user.SecurityStamp 作对比
|
|
|
|
var user = await UserManager.GetByIdAsync(CurrentUser.GetId()); |
|
|
|
var user = await GetCurrentUserAsync(); |
|
|
|
// 更换手机号
|
|
|
|
(await UserManager.ChangePhoneNumberAsync(user, input.NewPhoneNumber, input.Code)).CheckErrors(); |
|
|
|
|
|
|
|
|