From ca12247dbc0728c7900db4188565d4c4f7d907c4 Mon Sep 17 00:00:00 2001 From: username@email.com <yzy2002yzy@163.com> Date: 星期二, 29 八月 2023 09:25:54 +0800 Subject: [PATCH] Merge branch 'master' of http://47.108.235.38:8080/r/DocumentService --- DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs | 66 +++++++++++++++++++++++++++++---- 1 files changed, 58 insertions(+), 8 deletions(-) diff --git a/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs b/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs index 8988879..ce8b762 100644 --- a/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs +++ b/DocumentServiceAPI.Application/UserAndLogin/Services/TokenService.cs @@ -3,10 +3,13 @@ using Furion.Authorization; using Furion.DistributedIDGenerator; using Furion.JsonSerialization; +using Furion.Logging.Extensions; +using Microsoft.IdentityModel.Tokens; using NetTaste; using SqlSugar.Extensions; using System; using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; using System.Linq; using System.Reflection; using System.Security.Claims; @@ -18,7 +21,7 @@ /// <summary> /// token鏈嶅姟绫� /// </summary> - public class TokenService: IScoped + public class TokenService : IScoped { /// <summary> /// Token鏄暐 @@ -27,7 +30,7 @@ private ISqlSugarClient _db; private UserService _UserService; private IRedisCacheService _RedisCase; - public TokenService(ISqlSugarClient db, UserService UserInfoService, IRedisCacheService RedisCase ) { + public TokenService(ISqlSugarClient db, UserService UserInfoService, IRedisCacheService RedisCase) { _db = db; _UserService = UserInfoService; _RedisCase = RedisCase; @@ -36,9 +39,9 @@ /// 鏂板缓涓�涓猅Oken /// </summary> /// <returns></returns> - public string CreateToken(JwtInfo jwt) + public string CreateToken(JwtInfo jwt) { - string TokenKey=TokenKeys+ getTokenKey(jwt); + string TokenKey = TokenKeys + getTokenKey(jwt); IDictionary<string, object> propertyDictionary = new Dictionary<string, object>(); PropertyInfo[] properties = jwt.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); @@ -51,14 +54,57 @@ propertyDictionary.Add(propertyName.ToLower(), propertyValue); } var ID = IDGen.NextID(); - jwt.JID = ID; - var token= JWTEncryption.Encrypt(propertyDictionary, App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600); - if(jwt.LogInSource==LogInFrom.PC) - _RedisCase.Add<string>(TokenKey, ID.ToString(), expireSeconds: (int )(App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime??3600)); + jwt.JID = ID; + var token = JWTEncryption.Encrypt(propertyDictionary, App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600); + if (jwt.LogInSource == LogInFrom.PC) + _RedisCase.Add<string>(TokenKey, ID.ToString(), expireSeconds: (int)(App.GetConfig<JWTSettingsOptions>("JWTSettings").ExpiredTime ?? 3600)); return token; } + /// <summary> + /// 鏍¢獙token 鐨勬湁鏁堟�� + /// </summary> + /// <param name="Token"> Token鏍¢獙</param> + /// <param name="validationParameters"></param> + /// <param name="securityToken"></param> + /// <returns></returns> + public bool CheckJwt(string Token, out SecurityToken? securityToken) + { + var configuration = App.Configuration; + // var value = configuration["xxx:xxx"]; + + string secretKey = configuration["ZCUserInfoJwtOP:SecurityKey"]; + if (secretKey == null) + { + "娌℃湁閰嶇疆鐢ㄦ埛涓績鐨勫畨鍏ㄧ閽� ZCUserInfoJwtOP:SecurityKey 鎵句笉鍒�".LogInformation<TokenService>(); + throw Oops.Oh("閰嶇疆閿欒锛岃仈绯荤鐞嗗憳"); + } + JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler(); + //JwtSecurityToken jwt = tokenHandler.ReadJwtToken(Token); + + // 楠岃瘉 JWT 绛惧悕骞舵鏌ユ湁鏁堟湡 + TokenValidationParameters validationParameters = new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)), + ValidateIssuer = false, + ValidateAudience = false, + ClockSkew = TimeSpan.Zero // 绂佺敤鏃堕棿鍋忕Щ閲� + }; + try + { + ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(Token, validationParameters, out SecurityToken validatedToken); + + securityToken = validatedToken; + return true; + } + catch (SecurityTokenException) + { + securityToken=null; + return false; + } + } /// <summary> @@ -91,6 +137,10 @@ string TokenKey = TokenKeys + jwt.EID?.ToString() + jwt.UID?.ToString() + jwt.TEID?.ToString(); return TokenKey; } + + + + } } -- Gitblit v1.9.1