1
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
62
Yes.Application/Admins/Auths/ValidateCommandHandler.cs
Normal file
62
Yes.Application/Admins/Auths/ValidateCommandHandler.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
string CategoryRoute,
|
||||
string SearchRoute,
|
||||
string TagRoute,
|
||||
int TokenLifetimeMinutes,
|
||||
StorageSettingsResponse Storage
|
||||
);
|
||||
|
||||
|
||||
@@ -32,4 +32,8 @@ global using Yes.Infrastructure.Authorizations.Identity.Context;
|
||||
global using Yes.Infrastructure.Authorizations.Identity;
|
||||
global using Microsoft.Data.SqlClient;
|
||||
global using Yes.Infrastructure.Migrator.Providers;
|
||||
global using Yes.Infrastructure.Helpers;
|
||||
global using Yes.Infrastructure.Helpers;
|
||||
global using System.Security.Claims;
|
||||
global using System.IdentityModel.Tokens.Jwt;
|
||||
global using Microsoft.IdentityModel.Tokens;
|
||||
global using System.Text;
|
||||
Reference in New Issue
Block a user