From 8ff4f66f9f2cdf98da664d4b2941e23be322c8f6 Mon Sep 17 00:00:00 2001 From: zhougf Date: Mon, 8 Jul 2024 16:05:54 +0800 Subject: [PATCH] =?UTF-8?q?6.4.1=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HHWMS.Web/Controllers/HomeController.cs | 12 ++++++ HHWMS.Web/Controllers/LoginController.cs | 15 +++++++ HHWMS.Web/Program.cs | 1 + .../Support/SessionValidationMiddleware.cs | 38 +++++++++++++++++ HHWMS.Web/Support/UserSessionManger.cs | 42 +++++++++++++++++++ HHWMS.Web/Views/Home/Index.cshtml | 27 ++++++++++++ HHWMS.Web/Views/Login/Login.cshtml | 33 ++++++++++++++- .../Extensions/LoginUserInfoExtension.cs | 5 ++- version.props | 2 +- 9 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 HHWMS.Web/Support/SessionValidationMiddleware.cs create mode 100644 HHWMS.Web/Support/UserSessionManger.cs diff --git a/HHWMS.Web/Controllers/HomeController.cs b/HHWMS.Web/Controllers/HomeController.cs index 430e469..a85bdf4 100644 --- a/HHWMS.Web/Controllers/HomeController.cs +++ b/HHWMS.Web/Controllers/HomeController.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Reflection; using System.Text; using System.Web; +using HHWMS.Web.Support; +using HHWMS.Web.ViewModels.HomeVMs; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; @@ -152,7 +154,17 @@ namespace HHWMS.Web.Controllers else return JsonMore("No Data"); } + [Public] + public IActionResult CheckLogin() + { + return Ok(); + } + [Public] + public IActionResult BeforeLogin(LoginVM vm) + { + return JsonMore(UserSessionManager.AllSignined().ToList()); + } //[AllowAnonymous] //public github GetGithubInfo() //{ diff --git a/HHWMS.Web/Controllers/LoginController.cs b/HHWMS.Web/Controllers/LoginController.cs index 9f45cef..09922f4 100644 --- a/HHWMS.Web/Controllers/LoginController.cs +++ b/HHWMS.Web/Controllers/LoginController.cs @@ -14,6 +14,7 @@ using WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkUserVms; using HHWMS.Web.ViewModels.HomeVMs; using System.Collections.Generic; using System.Linq; +using HHWMS.Web.Support; namespace HHWMS.Web.Controllers { @@ -55,6 +56,11 @@ namespace HHWMS.Web.Controllers } else { + var signined = UserSessionManager.IsUserSignined(user.Name); + if (signined) + { + await KickUser(user.Name); + } //其他属性可以通过user.Attributes["aaa"] = "bbb"方式赋值 Wtm.LoginUserInfo = user; string url = string.Empty; @@ -78,6 +84,7 @@ namespace HHWMS.Web.Controllers } var principal = user.CreatePrincipal(); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, properties); + UserSessionManager.AddUserSession(user.Name, principal.Claims.First(c => c.Type == "SessionId").Value) return Redirect(HttpUtility.UrlDecode(url)); } } @@ -133,6 +140,14 @@ namespace HHWMS.Web.Controllers } } + [AllRights] + public async Task KickUser(string ITCode) + { + UserSessionManager.KickUser(ITCode); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + return Ok(); + } + [AllRights] [ActionDescription("ChangePassword")] public ActionResult ChangePassword() diff --git a/HHWMS.Web/Program.cs b/HHWMS.Web/Program.cs index b763197..1f62ec5 100644 --- a/HHWMS.Web/Program.cs +++ b/HHWMS.Web/Program.cs @@ -76,6 +76,7 @@ app.UseSession(); app.UseWtmSwagger(false); app.UseSwaggerExt(); app.UseWtm(); +app.UseMiddleware(); app.AddQuartzJobs(); app.UseEndpoints(endpoints => { diff --git a/HHWMS.Web/Support/SessionValidationMiddleware.cs b/HHWMS.Web/Support/SessionValidationMiddleware.cs new file mode 100644 index 0000000..794bb6e --- /dev/null +++ b/HHWMS.Web/Support/SessionValidationMiddleware.cs @@ -0,0 +1,38 @@ +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); + } +} \ No newline at end of file diff --git a/HHWMS.Web/Support/UserSessionManger.cs b/HHWMS.Web/Support/UserSessionManger.cs new file mode 100644 index 0000000..3c16139 --- /dev/null +++ b/HHWMS.Web/Support/UserSessionManger.cs @@ -0,0 +1,42 @@ +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using WalkingTec.Mvvm.Core; + +namespace HHWMS.Web.Support; + +using System.Collections.Concurrent; + +public class UserSessionManager +{ + private static ConcurrentDictionary userSessions = new ConcurrentDictionary(); + + public static void AddUserSession(string userId, string sessionId) + { + userSessions[userId.ToLower()] = sessionId; + } + + public static void RemoveUserSession(string userId) + { + userSessions.TryRemove(userId, out _); + } + + public static bool IsUserSessionValid(string userId, string sessionId) + { + return userSessions.TryGetValue(userId.ToLower(), out var storedSessionId) && storedSessionId == sessionId; + } + + public static bool IsUserSignined(string userId) + { + return userSessions.ContainsKey(userId); + } + + public static void KickUser(string userId) + { + userSessions.TryRemove(userId, out _); + } + + public static ConcurrentDictionary AllSignined() + { + return userSessions; + } +} \ No newline at end of file diff --git a/HHWMS.Web/Views/Home/Index.cshtml b/HHWMS.Web/Views/Home/Index.cshtml index b1d2e1d..f1535b4 100644 --- a/HHWMS.Web/Views/Home/Index.cshtml +++ b/HHWMS.Web/Views/Home/Index.cshtml @@ -1 +1,28 @@ + diff --git a/HHWMS.Web/Views/Login/Login.cshtml b/HHWMS.Web/Views/Login/Login.cshtml index cab682e..0f948bd 100644 --- a/HHWMS.Web/Views/Login/Login.cshtml +++ b/HHWMS.Web/Views/Login/Login.cshtml @@ -36,7 +36,7 @@ - + diff --git a/src/WalkingTec.Mvvm.Core/Extensions/LoginUserInfoExtension.cs b/src/WalkingTec.Mvvm.Core/Extensions/LoginUserInfoExtension.cs index 5b90d9f..2af8860 100644 --- a/src/WalkingTec.Mvvm.Core/Extensions/LoginUserInfoExtension.cs +++ b/src/WalkingTec.Mvvm.Core/Extensions/LoginUserInfoExtension.cs @@ -15,7 +15,10 @@ namespace WalkingTec.Mvvm.Core.Extensions public static ClaimsPrincipal CreatePrincipal(this LoginUserInfo self) { if (string.IsNullOrEmpty(self.ITCode)) throw new ArgumentException("Id is mandatory", nameof(self.ITCode)); - var claims = new List { new Claim(AuthConstants.JwtClaimTypes.Subject, self.ITCode) }; + var claims = new List { + new Claim(AuthConstants.JwtClaimTypes.Subject, self.ITCode), + new Claim("SessionId", Guid.NewGuid().ToString()) + }; if (!string.IsNullOrEmpty(self.Name)) { diff --git a/version.props b/version.props index e7c1969..0894dd8 100644 --- a/version.props +++ b/version.props @@ -1,6 +1,6 @@ - 6.3.9 + 6.4.1