服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-08-28 935722e71cc47d8c9f3b0e911de33c7eb0cbc693
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Rhea.Common;
using Rhea.Common.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tiger.IBusiness;
 
namespace Tiger.Business
{
    /// <summary>
    /// Tiger 项目激活类,包括以下文件:
    /// <br/>"项目名.cer"(Certification):包含有公钥的数字证书,以Xml格式存储
    /// <br/>"项目名.act"(ActivationCode):包含有激活码字符的文档
    ///  <br/>"项目名.req"(RequestCode):包含有请求码字符的文档
    /// </summary>
    public class TigerActive : Activation, ITigerActive
    {
        /// <summary>
        /// 使用激活码激活程序
        /// </summary>
        /// <param name="activationCode">激活码</param>
        /// <returns></returns>
        public override Result Active(string activationCode)
        {
            Result result = new(Result.Flags.Success, "软件激活成功!");
            try
            {
                License license = DecryptLicense(activationCode, GetCertification());
                if (license.IsNullOrEmpty())
                {
                    result.Flag = Result.Flags.Failed;
                    result.Message = $"激活失败,验证码解析出错。";
                }
                else
                {
                    //项目名 ProjectName
                    if (license.ProjectName != BizContext.CurrentProject)
                    {
                        result.Flag = Result.Flags.Failed;
                        result.Message = $"激活失败,验证码不适用于本项目。";
                    }
                    //机器码 MachineCode
                    if (license.MachineCode != BizContext.MachineCode)
                    {
                        result.Flag = Result.Flags.Failed;
                        result.Message = $"激活失败,验证码不适用于本软件。";
                    }
                    //有效期截至日期 ExpiryDate
                    if ((license.ExpiryDate.Date - DateTime.Now.Date).TotalMilliseconds < 0)
                    {
                        result.Flag = Result.Flags.Warning;
                        result.Message = $"激活失败,验证码已过期。";
                    }
                }
                if (!result.IsFailed)
                {
                    //保存激活码到注册表
                    SetActivationCode(activationCode);
                }
 
                if (result.IsSuccessed)
                {
                    Verify();
                }
            }
            catch (System.UnauthorizedAccessException ex)
            {
                result.CatchExceptionWithLog(ex, $"权限不足,请使用管理员运行后再激活");
            }
            catch (System.Exception ex)
            {
                result.CatchExceptionWithLog(ex, $"软件激活异常");
            }
            return result;
        }
 
        /// <summary>
        /// 验证当前程序是否已激活
        /// </summary>
        /// <returns></returns>
        public override Result Verify()
        {
            Result result = new(Result.Flags.Success, "授权验证通过!");
            try
            {
                var license = GetCurLicense();
                if (license.IsNullOrEmpty())
                {
                    result.Flag = Result.Flags.Failed;
                    result.Message = $"授权验证失败,未找到许可信息。";
                }
                else
                {
                    //项目名 ProjectName
                    if (!license.ProjectName.IsNullOrEmpty() && license.ProjectName != BizContext.CurrentProject)
                    {
                        result.Flag = Result.Flags.Failed;
                        result.Message = $"授权验证失败,项目未授权";
                    }
                    //机器码 MachineCode
                    if (!license.MachineCode.IsNullOrEmpty() && license.MachineCode != BizContext.MachineCode)
                    {
                        result.Flag = Result.Flags.Failed;
                        result.Message = $"授权验证失败,电脑未授权。";
                    }
                    //有效期截至日期 ExpiryDate
                    if (license.ExpiryDate != DateTime.MaxValue && (license.ExpiryDate.Date - DateTime.Now.Date).TotalMilliseconds < 0)
                    {
                        result.Flag = Result.Flags.Failed;
                        result.Message = $"授权验证失败,授权已过期。";
                    }
                    //工站字典,key:工站ID,value:允许同时开启的数量 StationDict
                    if (!license.StationDict.IsNullOrEmpty())
                    {
 
                    }
                    //试用期截至日期 TrialExpiryDate
                    if (!result.IsSuccessed && (license.TrialExpiryDate - DateTime.Now).TotalMilliseconds > 0)
                    {
                        result.Flag = Result.Flags.Success;
                        result.Message = $"授权验证通过,在试用期内。";
                    }
                }
            }
            catch (System.Exception ex)
            {
                result.CatchExceptionWithLog(ex, $"授权验证异常");
            }
            return result;
        }
 
        /// <summary>
        /// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中读取密钥证书
        /// </summary>
        /// <returns></returns>
        public string GetCertification()
        {
            Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
            return tc.Read("Certification");
        }
 
        /// <summary>
        /// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中写入密钥证书
        /// </summary>
        /// <param name="key">公钥</param>
        public void SetCertification(string key)
        {
            Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
            tc.Write("Certification", key);
        }
 
        /// <summary>
        /// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中读取激活码
        /// </summary>
        /// <returns></returns>
        public string GetActivationCode()
        {
            Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
            return tc.Read("ActivationCode");
        }
 
        /// <summary>
        /// 在注册表HKEY_LOCAL_MACHINE\SOFTWARE\TigerClouds节点中写入激活码
        /// </summary>
        /// <param name="code">激活码</param>
        public void SetActivationCode(string code)
        {
            Base.Regedit tc = new Base.Regedit(Base.Regedit.BaseKeys.LocalMachine, @"SOFTWARE\TigerClouds");
            tc.Write("ActivationCode", code);
        }
 
        /// <summary>
        /// 获取当前许可信息
        /// </summary>
        /// <returns></returns>
        public License GetCurLicense()
        {
            return DecryptLicense(GetActivationCode(), GetCertification());
        }
 
    }
}