8357WMS/HHWMS.Web/Support/SessionValidationMiddleware.cs
2024-06-27 00:51:33 +08:00

38 lines
1.1 KiB
C#

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)
{
var userId = 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);
}
}