Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

190 rindas
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Learun.Util;
  9. using Learun.Util.Common;
  10. using Newtonsoft.Json;
  11. namespace Quanjiang.DigitalScholl.WebLicense
  12. {
  13. public class LicenseStatus
  14. {
  15. public bool Result { get; set; }
  16. public string Message { get; set; }
  17. public string ExpirationDate { get; set; }
  18. }
  19. public class LicenseChecker
  20. {
  21. public static string GenerateMachineId()
  22. {
  23. string cpuid = ""; LicenseManager.GetCPUSerialNumber();
  24. string hd = LicenseManager.GetHardDriverNumber();
  25. string bios = ""; LicenseManager.GetBoisNumber();
  26. string uid = LicenseManager.GetMachineName();
  27. return DESEncrypt.Encrypt(cpuid + hd + bios + uid, "bjquanjiang@2020!");
  28. }
  29. public static void WriteLicense(string licensecode)
  30. {
  31. try
  32. {
  33. string binpath = AppDomain.CurrentDomain.BaseDirectory;
  34. string fullpath = binpath + "\\bin\\license.bjqj";
  35. System.IO.File.WriteAllText(fullpath, licensecode);
  36. }
  37. catch (Exception e)
  38. {
  39. throw e;
  40. }
  41. }
  42. public static LicenseStatus CheckLicense()
  43. {
  44. StreamReader sr = null;
  45. string machineid = string.Empty;
  46. LicenseStatus ls = new LicenseStatus();
  47. try
  48. {
  49. //Timeout timeout = new Timeout();
  50. //timeout.Do = GetMAManagementInfo;
  51. //bool bo = timeout.DoWithTimeout(new TimeSpan(0, 0, 0, 4));
  52. TimeOutCheckTool tool = new TimeOutCheckTool();
  53. var checkResult = tool.TimeoutCheckTwo(4000, () => GetMAManagementInfo());
  54. //var maResult = GetMAManagementInfo();
  55. if (!checkResult)
  56. {
  57. ls.Message = "未授权";
  58. return ls;
  59. }
  60. machineid = GenerateMachineId();
  61. //判断有无授权
  62. string binpath = AppDomain.CurrentDomain.BaseDirectory;
  63. string fullpath = binpath + "\\bin\\license.bjqj";
  64. if (System.IO.File.Exists(fullpath))
  65. {
  66. sr = new StreamReader(fullpath, Encoding.UTF8);
  67. string u7str = sr.ReadToEnd();
  68. if (!string.IsNullOrEmpty(u7str))
  69. {
  70. string desstr = DESEncrypt.Decrypt(u7str, "bjquanjiang@2020!@lckey" + Md5Helper.Encrypt(machineid, 32));
  71. if (!string.IsNullOrEmpty(desstr))
  72. {
  73. string[] codeanddate = desstr.Split('&');
  74. if (codeanddate[0] == Md5Helper.Encrypt(machineid, 16))
  75. {
  76. ls.ExpirationDate = codeanddate[1];
  77. if (!string.IsNullOrEmpty(codeanddate[1]))
  78. {
  79. ls.Result = LicenseManager.GetNowTime(codeanddate[1]);
  80. }
  81. else
  82. {
  83. ls.Result = true;
  84. ls.ExpirationDate = "永久有效";
  85. }
  86. ls.Message = ls.Result ? "已授权" : "授权期已过";
  87. }
  88. else
  89. {
  90. ls.Message = "未授权";
  91. }
  92. }
  93. else
  94. {
  95. ls.Message = "未授权";
  96. }
  97. }
  98. else
  99. {
  100. ls.Message = "未授权";
  101. }
  102. }
  103. else
  104. {
  105. ls.Message = "未找到授权文件";
  106. }
  107. return ls;
  108. }
  109. catch (Exception e)
  110. {
  111. ls.Result = false;
  112. ls.Message = "未授权,不正确的授权文件";
  113. return ls;
  114. }
  115. finally
  116. {
  117. if (sr != null)
  118. {
  119. sr.Dispose();
  120. sr.Close();
  121. }
  122. }
  123. }
  124. public static bool GetMAManagementInfo()
  125. {
  126. try
  127. {
  128. //查找授权管理信息
  129. var url = "http://123.57.209.16:8062/management/data";
  130. var MachineId = LicenseChecker.GenerateMachineId();
  131. var body = "{\"data\":'{\"code\":\"" + MachineId + "\",\"guid\":\"123\"}'}";
  132. Encoding encoding = Encoding.UTF8;
  133. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  134. request.Method = "POST";
  135. request.Accept = "text/html, application/xhtml+xml, */*";
  136. request.ContentType = "application/json";
  137. byte[] buffer = encoding.GetBytes(body);
  138. request.ContentLength = buffer.Length;
  139. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  140. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  141. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  142. {
  143. dynamic res = JsonConvert.DeserializeObject(reader.ReadToEnd());
  144. if (res.data == null)
  145. {
  146. return false;
  147. }
  148. else
  149. {
  150. bool Result = res.data;
  151. if (Result)
  152. {
  153. return true;
  154. }
  155. else
  156. {
  157. return false;
  158. }
  159. }
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. return true;
  165. }
  166. }
  167. }
  168. }