2026-05-21 15:52:36 +08:00
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
2026-05-22 14:29:22 +08:00
|
|
|
|
namespace FileShare_Services.Core
|
2026-05-21 15:52:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 鉴权服务抽象 —— 各宿主按自己的方式实现(JWT / Cookie / Token 等)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public interface IAuthService
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 验证请求并返回用户主体;返回 null 表示未授权。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
Task<ClaimsPrincipal?> AuthenticateAsync(ServiceEndpointContext context);
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检查当前用户是否有指定权限。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
Task<bool> AuthorizeAsync(ClaimsPrincipal user, string policy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 无需鉴权的默认实现(开发/公开 API 场景)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public sealed class AnonymousAuthService : IAuthService
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public Task<ClaimsPrincipal?> AuthenticateAsync(ServiceEndpointContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 匿名用户,始终通过
|
|
|
|
|
|
var identity = new ClaimsIdentity("anonymous");
|
|
|
|
|
|
return Task.FromResult<ClaimsPrincipal?>(new ClaimsPrincipal(identity));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public Task<bool> AuthorizeAsync(ClaimsPrincipal user, string policy)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|