You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

194 lines
6.5 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. #if DEBUG
  48. ls.Result = true;
  49. ls.ExpirationDate = "永久有效";
  50. ls.Message = "已授权";
  51. return ls;
  52. #endif
  53. try
  54. {
  55. TimeOutCheckTool tool = new TimeOutCheckTool();
  56. var checkResult = tool.TimeoutCheckTwo(4000, () => GetMAManagementInfo());
  57. //var maResult = GetMAManagementInfo();
  58. if (!checkResult)
  59. {
  60. ls.Message = "未授权";
  61. return ls;
  62. }
  63. machineid = GenerateMachineId();
  64. //判断有无授权
  65. string binpath = AppDomain.CurrentDomain.BaseDirectory;
  66. string fullpath = binpath + "\\bin\\license.bjqj";
  67. if (System.IO.File.Exists(fullpath))
  68. {
  69. sr = new StreamReader(fullpath, Encoding.UTF8);
  70. string u7str = sr.ReadToEnd();
  71. if (!string.IsNullOrEmpty(u7str))
  72. {
  73. string desstr = DESEncrypt.Decrypt(u7str, "bjquanjiang@2020!@lckey" + Md5Helper.Encrypt(machineid, 32));
  74. string ss = DESEncrypt.Encrypt("7f1ebe06e80c17f2&", "bjquanjiang@2020!@lckey" + Md5Helper.Encrypt(machineid, 32));
  75. if (!string.IsNullOrEmpty(desstr))
  76. {
  77. string[] codeanddate = desstr.Split('&');
  78. if (codeanddate[0] == Md5Helper.Encrypt(machineid, 16))
  79. {
  80. ls.ExpirationDate = codeanddate[1];
  81. if (!string.IsNullOrEmpty(codeanddate[1]))
  82. {
  83. ls.Result = LicenseManager.GetNowTime(codeanddate[1]);
  84. }
  85. else
  86. {
  87. ls.Result = true;
  88. ls.ExpirationDate = "永久有效";
  89. }
  90. ls.Message = ls.Result ? "已授权" : "授权期已过";
  91. }
  92. else
  93. {
  94. ls.Message = "未授权";
  95. }
  96. }
  97. else
  98. {
  99. ls.Message = "未授权";
  100. }
  101. }
  102. else
  103. {
  104. ls.Message = "未授权";
  105. }
  106. }
  107. else
  108. {
  109. ls.Message = "未找到授权文件";
  110. }
  111. return ls;
  112. }
  113. catch (Exception e)
  114. {
  115. ls.Result = false;
  116. ls.Message = "未授权,不正确的授权文件";
  117. return ls;
  118. }
  119. finally
  120. {
  121. if (sr != null)
  122. {
  123. sr.Dispose();
  124. sr.Close();
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 网络授权判断
  130. /// </summary>
  131. /// <returns></returns>
  132. public static bool GetMAManagementInfo()
  133. {
  134. try
  135. {
  136. //查找授权管理信息
  137. var url = "http://123.57.209.16:8062/management/data";
  138. var MachineId = LicenseChecker.GenerateMachineId();
  139. var body = "{\"data\":'{\"code\":\"" + MachineId + "\",\"guid\":\"123\"}'}";
  140. Encoding encoding = Encoding.UTF8;
  141. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  142. request.Method = "POST";
  143. request.Accept = "text/html, application/xhtml+xml, */*";
  144. request.ContentType = "application/json";
  145. byte[] buffer = encoding.GetBytes(body);
  146. request.ContentLength = buffer.Length;
  147. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  148. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  149. using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
  150. {
  151. dynamic res = JsonConvert.DeserializeObject(reader.ReadToEnd());
  152. if (res.data == null)
  153. {
  154. return false;
  155. }
  156. else
  157. {
  158. bool Result = res.data;
  159. if (Result)
  160. {
  161. return true;
  162. }
  163. else
  164. {
  165. return false;
  166. }
  167. }
  168. }
  169. }
  170. catch (Exception e)
  171. {
  172. return true;
  173. }
  174. }
  175. }
  176. }