You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Common.Utilities;
using djy.Model;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace djy_AfrApi.Milldlewares
{
/// <summary>
/// 处理特殊情况下的响应格式401、403
/// </summary>
public class UnifyResultMiddleware
{
private readonly RequestDelegate _next;
public UnifyResultMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
if (context.Response.StatusCode == 401 || context.Response.StatusCode == 403)
{
context.Response.ContentType = "application/json";
await context.Response
.WriteAsync(JsonConvert.SerializeObject(
new Response()
{
Code = context.Response.StatusCode,
Message = "授权验证失败或已过期,请重新登录!"
}))
.ConfigureAwait(false);
}
}
}
}