You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.4 KiB
101 lines
3.4 KiB
using Elight.Utility.Enum; |
|
using Microsoft.AspNetCore.Authentication; |
|
using Microsoft.AspNetCore.Http; |
|
using Microsoft.Extensions.Logging; |
|
using Microsoft.Extensions.Options; |
|
using Newtonsoft.Json; |
|
using Newtonsoft.Json.Serialization; |
|
using System.ComponentModel; |
|
using System.Net.Http.Headers; |
|
using System.Security.Claims; |
|
using System.Text; |
|
using System.Text.Encodings.Web; |
|
|
|
namespace Elight.Utility |
|
{ /// <summary> |
|
/// 响应认证处理器 |
|
/// </summary> |
|
/// Author:mxg |
|
/// CreatedTimed:2022-05-15 10:08 PM |
|
public class ResponseAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> |
|
{ |
|
#region Identity |
|
|
|
public ResponseAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, |
|
ILoggerFactory logger, |
|
UrlEncoder encoder, |
|
ISystemClock clock |
|
) : base(options, logger, encoder, clock) |
|
{ |
|
} |
|
|
|
#endregion Identity |
|
|
|
/// <summary> |
|
/// |
|
/// </summary> |
|
/// <returns></returns> |
|
/// <exception cref="NotImplementedException"></exception> |
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync() => throw new NotImplementedException(); |
|
|
|
/// <summary> |
|
/// HandleChallengeAsync |
|
/// </summary> |
|
/// <param name="properties"></param> |
|
/// <returns></returns> |
|
protected override async Task HandleChallengeAsync(AuthenticationProperties properties) |
|
{ |
|
Response.ContentType = "application/json"; |
|
Response.StatusCode = StatusCodes.Status401Unauthorized; |
|
await Response.WriteAsync(JsonConvert.SerializeObject( |
|
new ResponseStatusData |
|
{ |
|
Code = CustomizedStatusCodeEnum.Status401Unauthorized, |
|
Msg = CustomizedStatusCodeEnum.Status401Unauthorized.ToDescription() |
|
}, |
|
new JsonSerializerSettings() |
|
{ |
|
ContractResolver = new CamelCasePropertyNamesContractResolver() |
|
} |
|
)); |
|
} |
|
/// <summary> |
|
/// HandleForbiddenAsync |
|
/// </summary> |
|
/// <param name="properties"></param> |
|
/// <returns></returns> |
|
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties) |
|
{ |
|
Response.ContentType = "application/json"; |
|
Response.StatusCode = StatusCodes.Status403Forbidden; |
|
await Response.WriteAsync(JsonConvert.SerializeObject( |
|
new ResponseStatusData |
|
{ |
|
Code = CustomizedStatusCodeEnum.Status403Forbidden, |
|
Msg = CustomizedStatusCodeEnum.Status403Forbidden.ToDescription() |
|
}, |
|
new JsonSerializerSettings() |
|
{ |
|
ContractResolver = new CamelCasePropertyNamesContractResolver() |
|
} |
|
)); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// |
|
/// </summary> |
|
public class ResponseStatusData |
|
{ |
|
/// <summary> |
|
/// 状态码枚举 (0:操作失败 1:操作成功 401:未登录 403:权限不足 404:资源不存在 500:系统内部错误) |
|
/// <see cref="CustomizedStatusCodeEnum"/> |
|
/// </summary> |
|
public CustomizedStatusCodeEnum Code { get; set; } = CustomizedStatusCodeEnum.Status1Ok; |
|
|
|
/// <summary> |
|
/// |
|
/// </summary> |
|
public string? Msg { get; set; } |
|
} |
|
}
|
|
|