Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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