.net reactor使用教程(二)——代碼自動操作相關保護功能

下載.NET Reactor最新版試用>>

上篇已經學習了界面的各種功能以及各種配置,本次帶大家學習下代碼控制許可證。

代碼控制許可證的意思就是軟件經過.net reactor保護後,到期時客戶端就需要購買許可證,這時軟件開發商就需要生成許可證等操作,這個操作可以由代碼控制來達到自動化的效果。當然不僅僅是生成操作,還包擴獲取許可證的信息,作廢許可證,激活許可證等操作。

在安裝目錄下...\.NET Reactor\SDK\Binaries文件夾下可以找到License.dll和LicenseGen.dll(.net編寫 .net reactor是一個.net 編寫的程序)。其中License.dll主要用於獲取許可證信息,作廢許可證,激活許可證等,可整合到軟件項目中,而LicenseGen.dll主要用於生成許可證,不可整合,可以用於許可證自動生成的服務。

首先看下許可證生成代碼:

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// 創建許可證
/// </summary>
/// <param name="project_filename">
private void CreateLicenseFile()
{
    LicenseGenerator licensegen = new LicenseGenerator();
    licensegen.AddAdditonalLicenseInformation("Company", "Eye");
    licensegen.Hardware_Enabled = true;
    licensegen.HardwareID = "1234-1234-1234-1234-1234";
    licensegen.CreateLicenseFile(@"C:\newlicense.license");
}

這將會在c盤下生成newlicense.license許可證文件,文件的內容包括添加進去的鍵值對Company-Eye,開啓硬件鎖,此許可證只針對硬件編碼爲1234-1234-1234-1234-1234的機器有效。

再來看看License.dll的功能:

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
/// <summary>
/// 許可證是否可用
/// </summary>
/// <returns></returns>
private bool IsValidLicenseAvailable()
{
    return License.Status.Licensed;
}
 
/// <summary>
/// 獲取許可證鍵值信息
/// </summary>
private string ReadAdditonalLicenseInformation()
{
    string rtnStr = string.Empty;
    if (License.Status.Licensed)
    {
        for (int i = 0; i < License.Status.KeyValueList.Count; i++)
        {
            string key = License.Status.KeyValueList.GetKey(i).ToString();
            string value = License.Status.KeyValueList.GetByIndex(i).ToString();
 
            rtnStr += key + "-" + value + Environment.NewLine;
        }
    }
    return rtnStr;
}
 
/// <summary>
/// 獲取軟件鎖定信息
/// </summary>
/// <returns></returns>
private string ReadLockMsg()
{
    string rtnStr = string.Empty;
    //使用持續時間鎖
    bool lock_enabled = License.Status.Evaluation_Lock_Enabled;
    License.EvaluationType ev_type = License.Status.Evaluation_Type;
    int time = License.Status.Evaluation_Time;
    int time_current = License.Status.Evaluation_Time_Current;
    rtnStr += string.Format("是否開啓持續時間鎖:{0},規定使用最大持續時間{1},現在使用時間{2}\n",lock_enabled.ToString(),time.ToString(),time_current.ToString());
 
    //到期鎖
    bool lock_enabled1 = License.Status.Expiration_Date_Lock_Enable;
    System.DateTime expiration_date = License.Status.Expiration_Date;
    rtnStr += string.Format("是否開啓到期鎖:{0},到期時間{1}\n", lock_enabled1.ToString(), expiration_date.ToShortTimeString());
 
    //使用次數鎖
    bool lock_enabled2 = License.Status.Number_Of_Uses_Lock_Enable;
    int max_uses = License.Status.Number_Of_Uses;
    int current_uses = License.Status.Number_Of_Uses_Current;
    rtnStr += string.Format("是否開啓使用次數鎖:{0},最大使用次數{1},當前使用次數{2}\n", lock_enabled2.ToString(), max_uses.ToString(), current_uses.ToString());
 
    //併發運行鎖
    bool lock_enabled3 = License.Status.Number_Of_Instances_Lock_Enable;
    int max_instances = License.Status.Number_Of_Instances;
    rtnStr += string.Format("是否限制並行數量:{0},最大並行數量:{1}\n", lock_enabled3, max_instances.ToString());
 
    //硬件鎖
    bool lock_enabled4 = License.Status.Hardware_Lock_Enabled;
    string lic_hardware_id = "";
    if (lock_enabled)
    {
        lic_hardware_id = License.Status.License_HardwareID;
    }
    rtnStr += string.Format("證書是否開啓硬件鎖{0},對於特定硬件的有效{1}\n", lock_enabled4.ToString(), lic_hardware_id);
    return rtnStr;
}
 
/// <summary>
/// 獲取機器硬件編號
/// </summary>
/// <returns></returns>
private string GetHardwareID()
{
    return License.Status.HardwareID;
}
 
/// <summary>
/// 獲取許可證適用的硬件編碼
/// </summary>
/// <returns></returns>
private string GetLicenseHardwareID()
{
    return License.Status.License_HardwareID;
}
 
/// <summary>
/// 作廢許可證
/// </summary>
private string InvalidateLicense()
{
    string confirmation_code = License.Status.InvalidateLicense();
    return confirmation_code;
}
 
/// <summary>
/// 檢查作廢許可證的驗證碼是否有效
/// </summary>
/// <param name="confirmation_code">
/// <returns></returns>
public bool CheckConfirmationCode(string confirmation_code)
{
    return License.Status.CheckConfirmationCode(License.Status.HardwareID,
    confirmation_code);
}
 
/// <summary>
/// 重新激活許可證
/// </summary>
/// <param name="reactivation_code">
/// <returns></returns>
public bool ReactivateLicense(string reactivation_code)
{
    return License.Status.ReactivateLicense(reactivation_code);
}

其中作廢許可證及激活許可證的主要應用場景是:如果許可證開啓硬件鎖,客戶端想從一個機器移動許可證到另一個機器此時就需要先作廢許可證,然後在新機器裏重新激活許可證。作廢許可證可直接調用即可,但是激活許可證需要打開Tools->LicenseReactivation Tool來根據硬件編碼生成激活碼,傳入即可激活許可證。(這裏生成激活碼我只找到在工具裏可視化操作,在代碼中找不到這種方法,所以這個應用場景不太適合許可證全自動化的管理)。

下載.NET Reactor最新版試用>>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章