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.

57 lines
1.8 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();
11 months ago
if (endpoint?.Metadata.GetMetadata<IAllowAnonymous>() == null && context.Request.Path.Value.ToLower().Contains("/api/afr"))
11 months ago
{
var userId = context.User?.Claims?.FirstOrDefault(c => c.Type == "loginid")?.Value;
var user = _userService.GetUserInfo(userId);
if (user.Data == null)
{
11 months ago
Response result = new Response()
11 months ago
{
11 months ago
Code = 401,
Message = "登录过期(未查询到此用户),请重新登录!"
11 months ago
};
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);
}
}
}
}