SystemScriptHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Runtime.InteropServices;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Security.Principal;
  4. namespace IES.ExamServer.Helpers
  5. {
  6. public static class SystemScriptHelper
  7. {
  8. /// <summary>
  9. /// 检查是否管理员身份运行
  10. /// </summary>
  11. /// <returns></returns>
  12. public static bool IsAdministrator()
  13. {
  14. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  15. {
  16. // 获取当前用户的 Windows 身份
  17. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  18. // 创建一个 WindowsPrincipal 对象,用于表示当前用户的主体
  19. WindowsPrincipal principal = new WindowsPrincipal(identity);
  20. // 检查当前用户是否属于管理员组
  21. return principal.IsInRole(WindowsBuiltInRole.Administrator);
  22. }
  23. return false;
  24. }
  25. /// <summary>
  26. /// 根据域名在hosts文件中找到对于的ip地址。
  27. /// </summary>
  28. /// <param name="domain"></param>
  29. /// <returns></returns>
  30. public static (string? ip, string msg) FindIpAddressForDomain(string domain)
  31. {
  32. string? lastMatchingIp = null;
  33. try
  34. {
  35. string filePath = @"C:\Windows\System32\drivers\etc\hosts";
  36. string[] lines = File.ReadAllLines(filePath);
  37. foreach (string line in lines)
  38. {
  39. string trimmedLine = line.Trim();
  40. if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("#"))
  41. {
  42. continue;
  43. }
  44. string[] parts = trimmedLine.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  45. if (parts.Length >= 2)
  46. {
  47. string ip = parts[0];
  48. for (int i = 1; i < parts.Length; i++)
  49. {
  50. if (parts[i].Equals(domain, StringComparison.OrdinalIgnoreCase))
  51. {
  52. lastMatchingIp = ip;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. catch (Exception ex)
  59. {
  60. return (null, $"读取文件时出错: {ex.Message}");
  61. }
  62. return (lastMatchingIp, "匹配结果");
  63. }
  64. /// <summary>
  65. /// 检查证书是否安装,切是否过期,true 已经安装,false 未安装,用于检查证书是否需要重新安装,最终返回 true不用安装。
  66. /// 代码中使用的是 CurrentUser 存储位置,如果你需要检查计算机级别的证书存储区,可以将 StoreLocation.CurrentUser 替换为 StoreLocation.LocalMachine,但这可能需要管理员权限。
  67. /// </summary>
  68. /// <param name="certificate"></param>
  69. /// <returns></returns>
  70. public static bool CheckCertificate(string certificatePath)
  71. {
  72. bool installed = false, expired=false;
  73. X509Certificate2 certificate = new X509Certificate2(certificatePath);
  74. // 定义要检查的证书存储区
  75. StoreName[] storeNames = { StoreName.Root, StoreName.CertificateAuthority, StoreName.My };
  76. foreach (StoreName storeName in storeNames)
  77. {
  78. if (IsAdministrator())
  79. {
  80. using (X509Store store = new X509Store(storeName, StoreLocation.LocalMachine))
  81. {
  82. try
  83. {
  84. // 打开存储区
  85. store.Open(OpenFlags.ReadOnly);
  86. // 查找匹配的证书
  87. X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
  88. if (collection.Count > 0)
  89. {
  90. installed = true;
  91. var certificateInstalled = collection.First();
  92. expired = CheckCertificateExpired(certificateInstalled);
  93. break;
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. Console.WriteLine($"访问 {storeName} 存储区时出错: {ex.Message}");
  99. }
  100. finally
  101. {
  102. // 关闭存储区
  103. store.Close();
  104. }
  105. }
  106. }
  107. else {
  108. using (X509Store store = new X509Store(storeName, StoreLocation.CurrentUser))
  109. {
  110. try
  111. {
  112. // 打开存储区
  113. store.Open(OpenFlags.ReadOnly);
  114. // 查找匹配的证书
  115. X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
  116. if (collection.Count > 0)
  117. {
  118. installed = true;
  119. var certificateInstalled = collection.First();
  120. expired = CheckCertificateExpired(certificateInstalled);
  121. break;
  122. }
  123. }
  124. catch (Exception ex)
  125. {
  126. Console.WriteLine($"访问 {storeName} 存储区时出错: {ex.Message}");
  127. }
  128. finally
  129. {
  130. // 关闭存储区
  131. store.Close();
  132. }
  133. }
  134. }
  135. }
  136. return installed && !expired;
  137. }
  138. /// <summary>
  139. /// 检查证书是否过期,true 过期,false 未过期
  140. /// </summary>
  141. /// <param name="certificate"></param>
  142. /// <returns></returns>
  143. public static bool CheckCertificateExpired(X509Certificate2 certificate)
  144. {
  145. DateTime now = DateTime.Now;
  146. return now < certificate.NotBefore || now > certificate.NotAfter;
  147. }
  148. }
  149. }