mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-21 03:29:38 +08:00

* 服务器结构调整;添加 APIBearer * Server.exe 需要初始化用户密钥列表,UeerKeys 均需要小写用户名保存和读取 * 修复强制下线的目标客户端错误的问题
55 lines
2.3 KiB
C#
55 lines
2.3 KiB
C#
using System.Security.Claims;
|
|
using System.Text.Encodings.Web;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Options;
|
|
using Milimoe.FunGame.Server.Services;
|
|
|
|
namespace Milimoe.FunGame.WebAPI.Services
|
|
{
|
|
public class APIBearerAuthenticationHandler(IMemoryCache memoryCache, IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
|
|
{
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
{
|
|
// 检查是否有 Authorization Header
|
|
if (!Request.Headers.TryGetValue("Authorization", out Microsoft.Extensions.Primitives.StringValues value))
|
|
{
|
|
return AuthenticateResult.Fail("Authorization header is missing.");
|
|
}
|
|
|
|
string authorizationHeader = value.ToString();
|
|
if (!authorizationHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return AuthenticateResult.Fail("Invalid Authorization header format.");
|
|
}
|
|
|
|
string token = authorizationHeader["Bearer ".Length..].Trim();
|
|
|
|
// 验证 API Bearer Token
|
|
string key;
|
|
if (memoryCache.TryGetValue(FunGameSystem.FunGameWebAPITokenID, out object? cacheValue) && cacheValue is string str)
|
|
{
|
|
key = str;
|
|
}
|
|
else
|
|
{
|
|
key = FunGameSystem.GetAPISecretKey(FunGameSystem.FunGameWebAPITokenID);
|
|
memoryCache.Set(FunGameSystem.FunGameWebAPITokenID, key, TimeSpan.FromMinutes(5));
|
|
}
|
|
if (key == "" || token != key)
|
|
{
|
|
await Task.CompletedTask;
|
|
return AuthenticateResult.Fail("Invalid Token.");
|
|
}
|
|
|
|
// 如果验证通过,创建 ClaimsIdentity
|
|
Claim[] claims = [new Claim(ClaimTypes.Name, "FunGame Web API Claim")];
|
|
ClaimsIdentity identity = new(claims, Scheme.Name);
|
|
ClaimsPrincipal principal = new(identity);
|
|
AuthenticationTicket ticket = new(principal, Scheme.Name);
|
|
|
|
return AuthenticateResult.Success(ticket);
|
|
}
|
|
}
|
|
}
|