WMS/HHWMS.Web/Support/SessionValidationMiddleware.cs
2026-01-02 17:42:00 +08:00

41 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Support.Quartz;
namespace HHWMS.Web.Support;
public class SessionValidationMiddleware
{
private readonly RequestDelegate _next;
public SessionValidationMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.User.Identity.IsAuthenticated)
{
// 优先使用ITCode claim,如果不存在则使用Name
var userId = context.User.Claims.FirstOrDefault(c => c.Type == "ITCode")?.Value
?? context.User.Identity.Name;
var sessionId = context.User.Claims.FirstOrDefault(c => c.Type == "SessionId")?.Value;
if (!UserSessionManager.IsUserSessionValid(userId, sessionId))
{
// UserSessionManager.RemoveUserSession(userId);
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
context.Response.Redirect(new Configs().CookieOptions.LogoutPath);
return;
}
}
await _next(context);
}
}