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.

64 lines
2.2 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.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<IAllowAnonymous>() == null && context.Request.Path.Value.ToLower().Contains("/api/afr"))
{
// 因为ISF/AMS这步验证始终都无效所以这里先不做验证
//if (context.Request.Path.Value.Contains("/Load"))
//{
// var userId = context.User?.Claims?.FirstOrDefault(c => c.Type == "loginid")?.Value;
// var aut = _userService.GetUserAuthority(Guid.Parse(userId), "modAfrList");
//}
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);
}
}
}
}