Ai
1 Star 0 Fork 0

liming/Scheduler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ApiResponseHandler.cs 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
liming 提交于 2025-05-01 12:10 +08:00 . 完成所有JWT认证、授权以及登录接口
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Scheduler.Model;
using System.Text.Encodings.Web;
namespace Scheduler.Common
{
public class ApiResponseHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IUser _user;
public ApiResponseHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUser user) : base(options, logger, encoder, clock)
{
_user = user;
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
throw new NotImplementedException();
}
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
Response.ContentType = "application/json";
Response.StatusCode = StatusCodes.Status401Unauthorized;
await Response.WriteAsync(JsonConvert.SerializeObject((new ApiResponse(ResultStatus.NotAuthorizedToAccessInterface)).MessageModel));
}
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
{
Response.ContentType = "application/json";
if (_user.MessageModel != null)
{
Response.StatusCode =(int)_user.MessageModel.Status;
await Response.WriteAsync(JsonConvert.SerializeObject(_user.MessageModel));
}
else
{
Response.StatusCode = StatusCodes.Status403Forbidden;
await Response.WriteAsync(JsonConvert.SerializeObject((new ApiResponse(ResultStatus.InsufficientAccessPermissionLevel)).MessageModel));
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/cplmlm/scheduler.git
git@gitee.com:cplmlm/scheduler.git
cplmlm
scheduler
Scheduler
master

搜索帮助