This commit is contained in:
2025-05-13 00:13:02 +08:00
parent 4fa451be97
commit 9cc96bc66e
2 changed files with 5 additions and 18 deletions

View File

@@ -1,15 +1,15 @@
 
namespace Yes.Application.Admins.Auths namespace Yes.Application.Admins.Auths
{ {
public record ValidateCommand(string Token) : IRequest<ValidateCommandResponse>; public record ValidateCommand(string Token) : IRequest<ValidateCommandResponse>;
public record ValidateCommandResponse(string Token, IdentityInfo Identity, int TokenLifetimeMinutes); public record ValidateCommandResponse(string Token, IdentityInfo Identity, int TokenLifetimeMinutes);
public class ValidateCommandHandler(BlogDbContext db, IOptionsMonitor<BlogSettings> options) : IRequestHandler<ValidateCommand, ValidateCommandResponse> public class ValidateCommandHandler(BlogDbContext db, IOptionsMonitor<BlogSettings> options, IJwtProvider jwtProvider) : IRequestHandler<ValidateCommand, ValidateCommandResponse>
{ {
private readonly BlogDbContext _db = db; private readonly BlogDbContext _db = db;
private readonly BlogSettings _settings = options.CurrentValue; private readonly BlogSettings _settings = options.CurrentValue;
private readonly IJwtProvider _jwtProvider = jwtProvider;
public async Task<ValidateCommandResponse> Handle(ValidateCommand request, CancellationToken cancellationToken) public async Task<ValidateCommandResponse> Handle(ValidateCommand request, CancellationToken cancellationToken)
{ {
var token = request.Token; var token = request.Token;
@@ -20,21 +20,7 @@ namespace Yes.Application.Admins.Auths
try try
{ {
var securityKeyBase64 = JwtHelper.GenerateKey(_settings.SecretKey ?? ""); var claimsIdentity = await _jwtProvider.ReadToken(token);
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKeyBase64));
var jwtHander = new JwtSecurityTokenHandler();
var claimsIdentity = jwtHander.ValidateToken(token, new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = false,
ValidIssuer = "",
ValidAudience = "",
IssuerSigningKey = key,
ValidateLifetime = true,
}, out _);
var id = int.Parse(claimsIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value ?? "0"); var id = int.Parse(claimsIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value ?? "0");

View File

@@ -37,3 +37,4 @@ global using System.Security.Claims;
global using System.IdentityModel.Tokens.Jwt; global using System.IdentityModel.Tokens.Jwt;
global using Microsoft.IdentityModel.Tokens; global using Microsoft.IdentityModel.Tokens;
global using System.Text; global using System.Text;
global using Yes.Infrastructure.Authorizations.Identity.Authroizations;