This commit is contained in:
2025-05-12 23:57:05 +08:00
parent 10a344f6d6
commit 4fa451be97
57 changed files with 344 additions and 106 deletions

View File

@@ -2,12 +2,12 @@
{
public record UserLoginCommand(string Name, string Password) : IRequest<UserLoginCommandResponse>;
public record UserLoginCommandResponse(string Token, IdentityInfo Identity);
public record UserLoginCommandResponse(string Token, IdentityInfo Identity, int TokenLifetimeMinutes);
public class UserLoginCommandHandler(BlogDbContext db) : IRequestHandler<UserLoginCommand, UserLoginCommandResponse>
public class UserLoginCommandHandler(BlogDbContext db, IOptionsMonitor<BlogSettings> options) : IRequestHandler<UserLoginCommand, UserLoginCommandResponse>
{
private readonly BlogDbContext _db = db;
private readonly BlogSettings _settings = options.CurrentValue;
public async Task<UserLoginCommandResponse> Handle(UserLoginCommand request, CancellationToken cancellationToken)
{
var user = _db.Users.FirstOrDefault(x => x.Name == request.Name);
@@ -18,12 +18,23 @@
user.CheckPassword(request.Password);
return new UserLoginCommandResponse("", new IdentityInfo
var role = user.IsSystem ? IdentityRoleEnum.Admin : IdentityRoleEnum.User;
var claims = new[]
{
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim("Role", role.ToString()),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var token = JwtHelper.GenerateToken(claims, _settings.SecretKey, _settings.TokenLifetimeMinutes);
return new UserLoginCommandResponse(token, new IdentityInfo
{
Name = user.Name,
Id = user.Id,
Role = user.IsSystem ? IdentityRoleEnum.Admin : IdentityRoleEnum.User
});
Role = role
}, _settings.TokenLifetimeMinutes);
}
}

View File

@@ -0,0 +1,62 @@

namespace Yes.Application.Admins.Auths
{
public record ValidateCommand(string Token) : IRequest<ValidateCommandResponse>;
public record ValidateCommandResponse(string Token, IdentityInfo Identity, int TokenLifetimeMinutes);
public class ValidateCommandHandler(BlogDbContext db, IOptionsMonitor<BlogSettings> options) : IRequestHandler<ValidateCommand, ValidateCommandResponse>
{
private readonly BlogDbContext _db = db;
private readonly BlogSettings _settings = options.CurrentValue;
public async Task<ValidateCommandResponse> Handle(ValidateCommand request, CancellationToken cancellationToken)
{
var token = request.Token;
if (string.IsNullOrEmpty(token))
{
throw new InvalidTokenException();
}
try
{
var securityKeyBase64 = JwtHelper.GenerateKey(_settings.SecretKey ?? "");
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 user = await _db.Users.FindAsync(id);
if (user == null)
{
throw new InvalidTokenException();
}
var role = user.IsSystem ? IdentityRoleEnum.Admin : IdentityRoleEnum.User;
return new ValidateCommandResponse(token, new IdentityInfo
{
Name = user.Name,
Id = id,
Role = role
}, _settings.TokenLifetimeMinutes);
}
catch (Exception ex)
{
throw new InvalidTokenException();
}
}
}
}

View File

@@ -18,6 +18,7 @@
string CategoryRoute,
string SearchRoute,
string TagRoute,
int TokenLifetimeMinutes,
StorageSettingsResponse Storage
);