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#

11 months ago
using Common.Utilities;
using djy.Model;
12 months ago
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(
11 months ago
new Response()
12 months ago
{
11 months ago
Code = context.Response.StatusCode,
Message = "授权验证失败或已过期,请重新登录!"
12 months ago
}))
.ConfigureAwait(false);
}
}
}
}