Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

LicenseChecker.cs 5.9 KiB

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