修改 自动退出用户

IRC_NewDev
hang 2024-04-17 12:20:01 +08:00
parent fc579d9cf4
commit 7bf097c127
2 changed files with 19 additions and 8 deletions

View File

@ -60,37 +60,43 @@ public class LimitUserRequestAuthorization : IAsyncAuthorizationFilter
context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["LimitUser_AuthTokenMissing"])); context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["LimitUser_AuthTokenMissing"]));
} }
//2、在这里取缓存 进行比较 看是否有其他人进行了登陆,如果其他人登陆了,就把之前用户挤掉 //2、在这里取缓存 进行比较 看是否有其他人进行了登陆,如果其他人登陆了,就把之前用户挤掉
var cacheUserToken = (await _provider.GetAsync<string>(_userInfo.Id.ToString())).Value; var cacheUserToken = (await _provider.GetAsync<string>(_userInfo.Id.ToString())).Value;
//缓存中没有取到Token //缓存中没有取到Token
if (string.IsNullOrWhiteSpace(cacheUserToken)) if (string.IsNullOrWhiteSpace(cacheUserToken))
{ {
cacheUserToken = _userInfo.UserToken;
//设置当前用户最新Token //设置当前用户最新Token
await _provider.SetAsync(_userInfo.Id.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7)); await _provider.SetAsync(_userInfo.Id.ToString(), _userInfo.UserToken, TimeSpan.FromDays(7));
await _provider.SetAsync($"{_userInfo.Id.ToString()}_Online", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromMinutes(minutes));
cacheUserToken = _userInfo.UserToken;
} }
//是同一个人 //是同一个人
else if (cacheUserToken == _userInfo.UserToken) else if (cacheUserToken == _userInfo.UserToken)
{ {
var cacheTime = (await _provider.GetAsync<string>($"{_userInfo.Id.ToString()}_Online")).Value; var cacheTime = (await _provider.GetAsync<string>($"{_userInfo.Id.ToString()}_Online")).Value;
//过期了 需要自动退出 //过期了 需要自动退出
if(string.IsNullOrEmpty(cacheTime)) if (string.IsNullOrEmpty(cacheTime))
{ {
context.HttpContext.Response.ContentType = "application/json"; context.HttpContext.Response.ContentType = "application/json";
context.HttpContext.Response.StatusCode = StatusCodes.Status200OK; context.HttpContext.Response.StatusCode = StatusCodes.Status200OK;
context.Result = new JsonResult(ResponseOutput.NotOk("登录无操作超时自动退出"), ApiResponseCodeEnum.AutoLoginOut); context.Result = new JsonResult(ResponseOutput.NotOk("登录无操作超时自动退出", ApiResponseCodeEnum.AutoLoginOut));
} }
else else
{ {
await _provider.SetAsync($"{_userInfo.Id.ToString()}_Online", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromMinutes(minutes)); await _provider.SetAsync($"{_userInfo.Id.ToString()}_Online", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromMinutes(minutes));
} }
} }
else else

View File

@ -15,6 +15,7 @@ using Medallion.Threading;
using EasyCaching.Core; using EasyCaching.Core;
using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Contracts;
using LoginReturnDTO = IRaCIS.Application.Contracts.LoginReturnDTO; using LoginReturnDTO = IRaCIS.Application.Contracts.LoginReturnDTO;
using OfficeOpenXml.FormulaParsing.Utilities;
namespace IRaCIS.Application.Services namespace IRaCIS.Application.Services
{ {
@ -691,8 +692,12 @@ namespace IRaCIS.Application.Services
userLoginReturnModel.BasicInfo = loginUser; userLoginReturnModel.BasicInfo = loginUser;
// 登录 清除缓存 // 登录 清除缓存
_cache.Remove(userLoginReturnModel.BasicInfo.Id.ToString()); //_cache.Remove(userLoginReturnModel.BasicInfo.Id.ToString());
var userId = loginUser.Id;
await _cache.SetAsync($"{userId.ToString()}_Online", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), TimeSpan.FromMinutes(_verifyConfig.CurrentValue.AutoLoginOutMinutes));
return ResponseOutput.Ok(userLoginReturnModel); return ResponseOutput.Ok(userLoginReturnModel);