mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-22 03:59:36 +08:00

* 添加 Web API 和 RESTful API 模式; * 添加 SQLite 模式; * 添加 ISocketMessageProcessor 和 ISocketListener<> 接口,用于统一数据访问; * 重做了 ISocketModel; * 完善了 WebSocket 的连接模式。
34 lines
1.2 KiB
C#
34 lines
1.2 KiB
C#
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Milimoe.FunGame.Core.Library.Constant;
|
|
|
|
namespace Milimoe.FunGame.WebAPI.Services
|
|
{
|
|
public class JWTService(IConfiguration configuration)
|
|
{
|
|
public string GenerateToken(string username)
|
|
{
|
|
// 创建一个包含用户信息的声明
|
|
Claim[] claims = [
|
|
new Claim(JwtRegisteredClaimNames.Sub, username),
|
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
|
];
|
|
|
|
// 获取密钥和发行者
|
|
SymmetricSecurityKey key = new(General.DefaultEncoding.GetBytes(configuration["Jwt:Key"] ?? "undefined"));
|
|
SigningCredentials creds = new(key, SecurityAlgorithms.HmacSha256);
|
|
|
|
JwtSecurityToken token = new(
|
|
issuer: configuration["Jwt:Issuer"],
|
|
audience: configuration["Jwt:Audience"],
|
|
claims: claims,
|
|
expires: DateTime.Now.AddMinutes(30), // 设置过期时间
|
|
signingCredentials: creds
|
|
);
|
|
|
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
}
|
|
}
|
|
}
|