原文: .net reactor 學習系列(三)---.net reactor代碼自動操作相關保護功能
接上篇,上篇已經學習了界面的各種功能以及各種配置,這篇準備學習下代碼控制許可證。
代碼控制許可證的意思就是軟體經過.net reactor保護後,到期時用戶端就需要購買許可證,這時軟體開發商就需要生成許可證等操作,這個操作可以由代碼控制來達到自動化的效果。當然不僅僅是生成操作,還包擴擷取許可證的資訊,廢棄許可證,激活許可證等操作。
在安裝目錄下...\.NET Reactor\SDK\Binaries檔案夾下可以找到License.dll和LicenseGen.dll(.net編寫 .net reactor是一個.net 編寫的程式)。其中License.dll主要用于擷取許可證資訊,廢棄許可證,激活許可證等,可整合到軟體項目中,而LicenseGen.dll主要用于生成許可證,不可整合,可以用于許可證自動生成的服務。
首先看下許可證生成代碼:
/// <summary>
/// 建立許可證
/// </summary>
/// <param name="project_filename"></param>
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的功能:
/// <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"></param>
/// <returns></returns>
public bool CheckConfirmationCode(string confirmation_code)
{
return License.Status.CheckConfirmationCode(License.Status.HardwareID,
confirmation_code);
}
/// <summary>
/// 重新激活許可證
/// </summary>
/// <param name="reactivation_code"></param>
/// <returns></returns>
public bool ReactivateLicense(string reactivation_code)
{
return License.Status.ReactivateLicense(reactivation_code);
}
其中廢棄許可證及激活許可證的主要應用場景是:如果許可證開啟硬體鎖,用戶端想從一個機器移動許可證到另一個機器此時就需要先廢棄許可證,然後在新機器裡重新激活許可證。廢棄許可證可直接調用即可,但是激活許可證需要打開Tools->LicenseReactivation Tool來根據硬體編碼生成激活碼,傳入即可激活許可證。(這裡生成激活碼我隻找到在工具裡可視化操作,在代碼中找不到這種方法,是以這個應用場景不太适合許可證全自動化的管理)。
本文代碼下載下傳位址:
http://yunpan.cn/cJnMmILrX9Av2 %E8%AE%BF%E9%97%AE%E5%AF%86%E7%A0%81通路密碼 2585
2585下一篇主要學習下.net reactor的簡單應用場景