|
@@ -1,4 +1,5 @@
|
|
|
using System.Security.Claims;
|
|
|
+using Hotline.Identity;
|
|
|
using Hotline.Identity.Accounts;
|
|
|
using Hotline.Push;
|
|
|
using Hotline.Settings;
|
|
@@ -9,6 +10,7 @@ using IdentityModel;
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using XF.Domain.Authentications;
|
|
|
+using XF.Domain.Cache;
|
|
|
using XF.Domain.Dependency;
|
|
|
using XF.Domain.Exceptions;
|
|
|
using XF.Domain.Options;
|
|
@@ -23,6 +25,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
private readonly IRepository<User> _userRepository;
|
|
|
private readonly IJwtSecurity _jwtSecurity;
|
|
|
private readonly IOptionsSnapshot<IdentityConfiguration> _identityOptionsAccessor;
|
|
|
+ private readonly ITypedCache<AudienceTicket> _cacheAudience;
|
|
|
private readonly IMessageCodeDomainService _messageCodeDomainService;
|
|
|
|
|
|
public IdentityAppService(
|
|
@@ -31,6 +34,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
IRepository<User> userRepository,
|
|
|
IJwtSecurity jwtSecurity,
|
|
|
IOptionsSnapshot<IdentityConfiguration> identityOptionsAccessor,
|
|
|
+ ITypedCache<AudienceTicket> cacheAudience,
|
|
|
IMessageCodeDomainService messageCodeDomainService)
|
|
|
{
|
|
|
_accountRepository = accountRepository;
|
|
@@ -38,6 +42,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
_userRepository = userRepository;
|
|
|
_jwtSecurity = jwtSecurity;
|
|
|
_identityOptionsAccessor = identityOptionsAccessor;
|
|
|
+ _cacheAudience = cacheAudience;
|
|
|
_messageCodeDomainService = messageCodeDomainService;
|
|
|
}
|
|
|
|
|
@@ -50,7 +55,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
throw UserFriendlyException.SameMessage("用户名或密码错误!");
|
|
|
|
|
|
//校验验证码
|
|
|
- await _messageCodeDomainService.CheckdCode(account.UserName, account.PhoneNo, dto.MsgCode, cancellationToken);
|
|
|
+ //await _messageCodeDomainService.CheckdCode(account.UserName, account.PhoneNo, dto.MsgCode, cancellationToken);
|
|
|
|
|
|
if (account.Status != EAccountStatus.Normal)
|
|
|
throw UserFriendlyException.SameMessage("用户名或密码错误!");
|
|
@@ -68,7 +73,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
await _accountRepository.UpdateAsync(account, cancellationToken);
|
|
|
throw UserFriendlyException.SameMessage("账号名或密码错误!");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//限制系统类型账户频繁获取token的行为
|
|
|
//todo
|
|
|
|
|
@@ -88,7 +93,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
var jwtOptions = _identityOptionsAccessor.Value.Jwt;
|
|
|
var claims = new List<Claim>
|
|
|
{
|
|
|
- //new(JwtClaimTypes.Id, account.Id),
|
|
|
+ new(JwtClaimTypes.Id, account.Id),
|
|
|
new(JwtClaimTypes.Subject, account.Id),
|
|
|
new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
|
|
|
new(AppClaimTypes.UserDisplayName, account.Name),
|
|
@@ -112,7 +117,10 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
|
|
|
);
|
|
|
}
|
|
|
claims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
|
|
|
- var token = _jwtSecurity.EncodeJwtToken(claims);
|
|
|
+ var audience = new AudienceTicket(account.Id);
|
|
|
+ var expiredSeconds = jwtOptions.Expired <= 0 ? 3600 : jwtOptions.Expired;
|
|
|
+ await _cacheAudience.SetAsync(audience.Id, audience, TimeSpan.FromSeconds(expiredSeconds), cancellationToken);
|
|
|
+ var token = _jwtSecurity.EncodeJwtToken(claims, audience.Ticket);
|
|
|
return token;
|
|
|
}
|
|
|
}
|