1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using DocumentServiceAPI.Model;
using DocumentServiceAPI.Services.IService;
using Furion.Authorization;
using Furion.DistributedIDGenerator;
using Furion.JsonSerialization;
using NetTaste;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
 
namespace DocumentServiceAPI.Application.UserAndLogin.Services
{
    /// <summary>
    /// token服务类
    /// </summary>
    public  class TokenService: IScoped
    {
        /// <summary>
        /// Token是啥
        /// </summary>
        static string TokenKeys = "TokenKeys:";
        private ISqlSugarClient _db;
        private UserService _UserService;
        private IRedisCacheService _RedisCase;
        public TokenService(ISqlSugarClient db, UserService  UserInfoService, IRedisCacheService RedisCase ) {
            _db = db;
            _UserService = UserInfoService;
            _RedisCase = RedisCase;
        }
        /// <summary>
        /// 新建一个TOken 
        /// </summary>
        /// <returns></returns>
        public string  CreateToken(JwtInfo jwt)
        {
            string TokenKey=TokenKeys+ getTokenKey(jwt);
            IDictionary<string, object> propertyDictionary = new Dictionary<string, object>();
 
            PropertyInfo[] properties = jwt.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
 
            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;
                object propertyValue = property.GetValue(jwt);
 
                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));
            return token;
        }
 
 
 
 
        /// <summary>
        /// 校验jwt信息是否有效,判决单端登录验证
        /// </summary>
        /// <returns></returns>
         public bool  CheckToken()
        {
           
       var Jwtinfo=     _UserService.GetJwtInfo();
 
        var Key=    getTokenKey(Jwtinfo);
            
        string T  = _RedisCase.Get<string>(Key);
            if (T == null)
                return true;
            if (T != Jwtinfo.JID.ToString())
                return false;
            return true;
              
 
        }
        /// <summary>
        /// 获取TokenKey
        /// </summary>
        /// <param name="jwt"></param>
        /// <returns></returns>
        private string getTokenKey(JwtInfo jwt)
        {
            string TokenKey = TokenKeys + jwt.EID?.ToString() + jwt.UID?.ToString()  + jwt.TID?.ToString();
            return TokenKey;
        }
    }
   
}