添加在线用户管理,同一账号只允许一台设备登陆。

后期可限制登陆用户人数等功能
This commit is contained in:
zhougf committed 2024-06-27 00:51:33 +08:00
1 parent c67f0fb317
commit 6f38757a3f
10 files changed
+189 -7

No files matched your search

+16
View File
@@ -3,11 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
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;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -153,6 +157,18 @@ 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()
//{
+27 -2
View File
@@ -14,6 +14,11 @@ using WalkingTec.Mvvm.Mvc.Admin.ViewModels.FrameworkUserVms;
using HHWMS.Web.ViewModels.HomeVMs;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HHWMS.Web.Support;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using WalkingTec.Mvvm.Core.Support.Quartz;
namespace HHWMS.Web.Controllers
{
@@ -33,7 +38,7 @@ namespace HHWMS.Web.Controllers
}
return View(vm);
}
[Public]
[HttpPost]
public async Task<ActionResult> Login(LoginVM vm)
@@ -47,7 +52,9 @@ namespace HHWMS.Web.Controllers
return View(vm);
}
}*/
var user = Wtm.DoLogin(vm.ITCode, vm.Password, vm.Tenant);
if (user == null)
{
vm.MSD.AddModelError("", Localizer["Sys.LoginFailed"]);
@@ -55,6 +62,13 @@ namespace HHWMS.Web.Controllers
}
else
{
var bb = UserSessionManager.IsUserSignined(user.Name);
if (bb)
{
// 踢人下线
await KickUser(user.Name);
}
//其他属性可以通过user.Attributes["aaa"] = "bbb"方式赋值
Wtm.LoginUserInfo = user;
string url = string.Empty;
@@ -77,7 +91,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));
}
}
@@ -120,6 +136,7 @@ namespace HHWMS.Web.Controllers
[ActionDescription("Logout")]
public async Task Logout()
{
UserSessionManager.RemoveUserSession(Wtm.LoginUserInfo.Name);
HttpContext.Session.Clear();
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
if (ConfigInfo.HasMainHost == false || Wtm.LoginUserInfo?.CurrentTenant != null)
@@ -132,7 +149,15 @@ namespace HHWMS.Web.Controllers
HttpContext.Response.Redirect(ConfigInfo.MainHost);
}
}
[AllRights]
public async Task<IActionResult> KickUser(string ITcode)
{
UserSessionManager.KickUser(ITcode);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return Ok();
}
[AllRights]
[ActionDescription("ChangePassword")]
public ActionResult ChangePassword()