feat: 初始化后台管理模块与基础业务框架
1. 新增依赖注入生命周期标记接口、基础仓储与管理员仓储实现 2. 新增管理员认证与用户服务接口,补充认证相关DTO 3. 重构实体审计字段命名,统一Created/UpdatedAt规范 4. 新增大量业务实体类与API版本枚举配置 5. 集成Autofac依赖注入、JWT自动刷新与跨域配置 6. 替换原有微信小程序与旧认证服务为后台管理系统架构 7. 完善Swagger文档配置与项目基础部署配置
This commit is contained in:
@ -44,28 +44,51 @@ public static class JwtHelper
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证JWT令牌
|
||||
/// 获取Token过期时间
|
||||
/// </summary>
|
||||
/// <param name="token">JWT令牌</param>
|
||||
/// <param name="settings">JWT配置</param>
|
||||
/// <returns>ClaimsPrincipal对象</returns>
|
||||
public static ClaimsPrincipal ValidateToken(string token, JwtSettings settings)
|
||||
/// <returns>过期时间</returns>
|
||||
public static DateTime? GetTokenExpiry(string token)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
var key = Encoding.UTF8.GetBytes(settings.SecretKey!);
|
||||
|
||||
var validationParameters = new TokenValidationParameters
|
||||
if (tokenHandler.ReadToken(token) is JwtSecurityToken jwtToken)
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = settings.Issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = settings.Audience,
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateLifetime = true,
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
return jwtToken.ValidTo;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return tokenHandler.ValidateToken(token, validationParameters, out _);
|
||||
/// <summary>
|
||||
/// 从Token中获取用户ID
|
||||
/// </summary>
|
||||
/// <param name="token">JWT令牌</param>
|
||||
/// <returns>用户ID</returns>
|
||||
public static long? GetUserIdFromToken(string token)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
if (tokenHandler.ReadToken(token) is JwtSecurityToken jwtToken)
|
||||
{
|
||||
var userIdClaim = jwtToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
|
||||
if (long.TryParse(userIdClaim?.Value, out long userId))
|
||||
{
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Token中获取用户名
|
||||
/// </summary>
|
||||
/// <param name="token">JWT令牌</param>
|
||||
/// <returns>用户名</returns>
|
||||
public static string GetUserNameFromToken(string token)
|
||||
{
|
||||
var tokenHandler = new JwtSecurityTokenHandler();
|
||||
if (tokenHandler.ReadToken(token) is JwtSecurityToken jwtToken)
|
||||
{
|
||||
return jwtToken.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value ?? string.Empty;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user