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
{
///
/// 响应认证处理器
///
public class ResponseAuthenticationHandler : AuthenticationHandler
{
public ResponseAuthenticationHandler(
IOptionsMonitor options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock
) : base(options, logger, encoder, clock)
{
}
protected override Task 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; }
}
}