From 209e88a905cfd4c4c6cc723fab64a7398a2e63f0 Mon Sep 17 00:00:00 2001 From: zhougf Date: Mon, 8 Jul 2024 15:58:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=9C=A8=E7=BA=BF=E7=AE=A1?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E4=B8=80=E4=B8=AA=E8=B4=A6=E5=8F=B7=E5=8F=AA?= =?UTF-8?q?=E5=87=86=E5=9C=A8=E4=B8=80=E5=8F=B0=E8=AE=BE=E5=A4=87=E4=B8=8A?= =?UTF-8?q?=E7=99=BB=E9=99=86=EF=BC=8C=E5=85=B6=E4=BB=96=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=99=BB=E9=99=86=E5=90=8E=EF=BC=8C=E6=AD=A4=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HHWMS.Web/Controllers/HomeController.cs | 13 ++++++ HHWMS.Web/Controllers/LoginController.cs | 19 ++++++++- HHWMS.Web/HHWMS.Web.csproj | 6 +-- 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 ++++++++++++++- HHWMS.Web/appsettings.json | 18 ++++---- 9 files changed, 182 insertions(+), 15 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..a734c2f 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; @@ -153,6 +155,17 @@ namespace HHWMS.Web.Controllers 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..0b699d7 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 { @@ -48,13 +49,19 @@ namespace HHWMS.Web.Controllers } }*/ var user = Wtm.DoLogin(vm.ITCode, vm.Password, vm.Tenant); - if (user == null) + if (user == null) { vm.MSD.AddModelError("", Localizer["Sys.LoginFailed"]); return View(vm); } else { + var signined = UserSessionManager.IsUserSignined(user.Name); + if (signined) + { + // 踢人下线 + await KickUser(user.Name); + } //其他属性可以通过user.Attributes["aaa"] = "bbb"方式赋值 Wtm.LoginUserInfo = user; string url = string.Empty; @@ -77,7 +84,9 @@ 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 +142,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/HHWMS.Web.csproj b/HHWMS.Web/HHWMS.Web.csproj index 5f08f3f..cf7aa1f 100644 --- a/HHWMS.Web/HHWMS.Web.csproj +++ b/HHWMS.Web/HHWMS.Web.csproj @@ -71,9 +71,9 @@ - - - + + + diff --git a/HHWMS.Web/Program.cs b/HHWMS.Web/Program.cs index 2b5c3aa..2ac92c6 100644 --- a/HHWMS.Web/Program.cs +++ b/HHWMS.Web/Program.cs @@ -82,6 +82,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..92f48a8 100644 --- a/HHWMS.Web/Views/Home/Index.cshtml +++ b/HHWMS.Web/Views/Home/Index.cshtml @@ -1 +1,28 @@ + \ No newline at end of file diff --git a/HHWMS.Web/Views/Login/Login.cshtml b/HHWMS.Web/Views/Login/Login.cshtml index cab682e..f061bca 100644 --- a/HHWMS.Web/Views/Login/Login.cshtml +++ b/HHWMS.Web/Views/Login/Login.cshtml @@ -36,7 +36,7 @@ - + diff --git a/HHWMS.Web/appsettings.json b/HHWMS.Web/appsettings.json index 30322fa..d3f6eb2 100644 --- a/HHWMS.Web/appsettings.json +++ b/HHWMS.Web/appsettings.json @@ -22,17 +22,17 @@ }, "Connections": [ - //{ - // "Key": "default", - // "Value": "Server=127.0.0.1;Port=3306;Database=WMS;Uid=root;Password=abc123*;Character Set=utf8mb4;", - // "DBType": "mysql" - //}, { "Key": "default", - "Value": "Data Source=.;Initial Catalog=WMS;User=sa;Password=abc123*;Persist Security Info=True", - "DbContext": "DataContext", - "DBType": "sqlserver" //DataBase, you can choose mysql,sqlserver,pgsql,sqlite,oracle - } + "Value": "Server=127.0.0.1;Port=3306;Database=WMS;Uid=root;Password=abc123*;Character Set=utf8mb4;", + "DBType": "mysql" + }, +// { +// "Key": "default", +// "Value": "Data Source=.;Initial Catalog=WMS;User=sa;Password=abc123*;Persist Security Info=True", +// "DbContext": "DataContext", +// "DBType": "sqlserver" //DataBase, you can choose mysql,sqlserver,pgsql,sqlite,oracle +// } //{ // "Key": "default", // "Value": "Data Source=10.18.106.4;Initial Catalog=8357wms;User=wms;Password=abc123*;Persist Security Info=True",