using Common.DJYModel; using Common.Utilities; using djy.IService.Djy; using djy.Model; using djy.Service; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; using System; using System.Linq; using System.Text.Json; using System.Threading.Tasks; namespace djy_AfrApi.Milldlewares { public class NextAuthorizationMiddleware { private readonly RequestDelegate _next; private readonly IDjyUserService _userService; public NextAuthorizationMiddleware(RequestDelegate next, IDjyUserService userService) { _next = next; _userService = userService; } public async Task InvokeAsync(HttpContext context) { var endpoint = context.GetEndpoint(); if (endpoint?.Metadata.GetMetadata() == null && context.Request.Path.Value.ToLower().Contains("/api/afr")) { var userId = context.User?.Claims?.FirstOrDefault(c => c.Type == "loginid")?.Value; var user = _userService.GetUserInfo(userId); if (user.Data == null) { Response result = new Response() { Code = 401, Message = "登录过期(未查询到此用户),请重新登录!" }; context.Response.ContentType = "application/json"; await context.Response.WriteAsync(JsonConvert.SerializeObject(result)).ConfigureAwait(false); } else { context.Items["CurrentUser"] = user.Data; await _next(context); } } else { await _next(context); } } } }