1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using Admin.Core.Common.Extensions;
- 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;
- using System.Text.Encodings.Web;
- using System.Threading.Tasks;
- using StatusCodes = Admin.Core.Enums.StatusCodes;
- namespace Admin.Core.Auth
- {
- /// <summary>
- /// 响应认证处理器
- /// </summary>
- public class ResponseAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
- {
- public ResponseAuthenticationHandler(
- IOptionsMonitor<AuthenticationSchemeOptions> options,
- ILoggerFactory logger,
- UrlEncoder encoder,
- ISystemClock clock
- ) : base(options, logger, encoder, clock)
- {
- }
- protected override Task<AuthenticateResult> HandleAuthenticateAsync()
- {
- throw new NotImplementedException();
- }
- protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
- {
- Response.ContentType = "application/json";
- Response.StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status401Unauthorized;
- await Response.WriteAsync(JsonConvert.SerializeObject(
- new ResponseStatusData
- {
- Code = StatusCodes.Status401Unauthorized,
- Msg = StatusCodes.Status401Unauthorized.ToDescription()
- },
- new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- }
- ));
- }
- protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
- {
- Response.ContentType = "application/json";
- Response.StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status403Forbidden;
- await Response.WriteAsync(JsonConvert.SerializeObject(
- new ResponseStatusData
- {
- Code = StatusCodes.Status403Forbidden,
- Msg = StatusCodes.Status403Forbidden.ToDescription()
- },
- new JsonSerializerSettings()
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- }
- ));
- }
- }
- public class ResponseStatusData
- {
- public StatusCodes Code { get; set; } = StatusCodes.Status1Ok;
- public string Msg { get; set; }
- }
- }
|