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#

11 months ago
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)
{
// 因为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)
{
MessageModel result = new MessageModel()
{
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);
}
}
}
}