|
@@ -0,0 +1,162 @@
|
|
|
+// <author></author>
|
|
|
+// <date></date>
|
|
|
+// <description></description>
|
|
|
+
|
|
|
+using ERP.Framework.Cache;
|
|
|
+using ERP.Framework.Config;
|
|
|
+using ERP.Framework.Constants;
|
|
|
+using ERP.Framework.Emum;
|
|
|
+using ERP.Framework.Security.Core;
|
|
|
+using ERP.Framework.Utils;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace ERP.Framework.Security
|
|
|
+{
|
|
|
+ public class LoginHelper
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// 根据设备进行登录
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userInfo"></param>
|
|
|
+ /// <param name="securityConfig"></param>
|
|
|
+ /// <param name="device"></param>
|
|
|
+ /// <param name="deviceKey"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static JwtToken LoginByDevice(
|
|
|
+ LoginUser userInfo
|
|
|
+ , SecurityConfig securityConfig
|
|
|
+ , DeviceEnum device
|
|
|
+ , string deviceKey)
|
|
|
+ {
|
|
|
+ string tokenId;
|
|
|
+
|
|
|
+ var tokenList = GetTokenList(userInfo.UserId);
|
|
|
+
|
|
|
+ var token = TokenHelper.CreateToken(
|
|
|
+ securityConfig.JwtSecurityKey
|
|
|
+ , userInfo.UserId
|
|
|
+ , userInfo.UserName
|
|
|
+ , securityConfig.TimeOut
|
|
|
+ , out tokenId);
|
|
|
+
|
|
|
+
|
|
|
+ //Todo 不允许并发登录处理
|
|
|
+ //Todo 处理超出的会话
|
|
|
+
|
|
|
+ tokenList.Add(new TokenSign
|
|
|
+ {
|
|
|
+ TokenId = tokenId,
|
|
|
+ Device = device
|
|
|
+ });
|
|
|
+
|
|
|
+ SetTokenList(userInfo.UserId, tokenList);
|
|
|
+
|
|
|
+ SetLastActivity(tokenId, securityConfig.ActivityTimeOut, securityConfig.TimeOut);
|
|
|
+
|
|
|
+ SetUserInfo(tokenId, userInfo, securityConfig.TimeOut);
|
|
|
+
|
|
|
+ var result = new JwtToken(FrameworkConstant.TOKEN_PREFIX + " " + token, securityConfig.TimeOut);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //public static LoginUser GetLoginUser()
|
|
|
+ //{
|
|
|
+ // var httpContextAccessor = new HttpContextAccessor();
|
|
|
+ // var httpContext = httpContextAccessor.HttpContext;
|
|
|
+ // var loginUser = httpContext!.Items["LoginUser"] as LoginUser;
|
|
|
+
|
|
|
+ // if (loginUser != null)
|
|
|
+ // {
|
|
|
+ // return loginUser;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // var tokenId = httpContext!.Items["TokenId"] as string;
|
|
|
+ // var redisData = RedisHelper.Get(AuthConstant.UserInfo + tokenId);
|
|
|
+ // var result = JsonConvert.DeserializeObject<LoginUser>(redisData);
|
|
|
+ // httpContext.Items["LoginUser"] = result;
|
|
|
+ // return result;
|
|
|
+ //}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取TokenList缓存
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="userId"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private static List<TokenSign> GetTokenList(long userId)
|
|
|
+ {
|
|
|
+ var tokenListData = RedisHelper.Get(AuthConstant.TOKEN_LIST + CryptoUtil.MD5(userId.ToString()));
|
|
|
+
|
|
|
+ if (!tokenListData.IsNullOrEmpty())
|
|
|
+ {
|
|
|
+ var data = JsonConvert.DeserializeObject<List<TokenSign>>(tokenListData);
|
|
|
+ return data;`
|
|
|
+ }
|
|
|
+
|
|
|
+ return new List<TokenSign>();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建Last Activity
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tokenId"></param>
|
|
|
+ /// <param name="activityTimeOut"></param>
|
|
|
+ private static void SetLastActivity(
|
|
|
+ string tokenId
|
|
|
+ , int activityTimeOut
|
|
|
+ , int tokenTimeOut)
|
|
|
+ {
|
|
|
+ var key = AuthConstant.LAST_ACTIVITY + tokenId;
|
|
|
+ var val = DateTime.Now.AddMinutes(activityTimeOut).ToDateLongString();
|
|
|
+ RedisHelper.SaveExpire(key, val, tokenTimeOut);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 退出用户
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tokenId"></param>
|
|
|
+ /// <param name="activityTimeOut"></param>
|
|
|
+ private static void LogoutUser(
|
|
|
+ string tokenId
|
|
|
+ , int tokenTimeOut)
|
|
|
+ {
|
|
|
+ var key = AuthConstant.LAST_ACTIVITY + tokenId;
|
|
|
+ RedisHelper.SaveExpire(key, AuthConstant.SUBSTITUTED, tokenTimeOut);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建Token List缓存
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tokenId"></param>
|
|
|
+ /// <param name="activityTimeOut"></param>
|
|
|
+ private static void SetTokenList(
|
|
|
+ long userId
|
|
|
+ , List<TokenSign> list)
|
|
|
+ {
|
|
|
+ var key = AuthConstant.TOKEN_LIST + CryptoUtil.MD5(userId.ToString());
|
|
|
+ var val = JsonConvert.SerializeObject(list);
|
|
|
+ RedisHelper.Save(key, val);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 创建UserInfo
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tokenId"></param>
|
|
|
+ /// <param name="activityTimeOut"></param>
|
|
|
+ private static void SetUserInfo(
|
|
|
+ string tokenId
|
|
|
+ , LoginUser loginUser
|
|
|
+ , int tokenTimeOut)
|
|
|
+ {
|
|
|
+ var key = AuthConstant.USER_INFO + tokenId;
|
|
|
+ var val = JsonConvert.SerializeObject(loginUser);
|
|
|
+ RedisHelper.SaveExpire(key, val, tokenTimeOut);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|