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.

38 lines
1.1 KiB
C#

12 months ago
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 MessageModel()
{
code = context.Response.StatusCode,
message = "授权验证失败或已过期,请重新登录!"
}))
.ConfigureAwait(false);
}
}
}
}