123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System.Runtime.InteropServices;
- using System.Security.Cryptography.X509Certificates;
- using System.Security.Principal;
- namespace IES.ExamServer.Helpers
- {
- public static class SystemScriptHelper
- {
- /// <summary>
- /// 检查是否管理员身份运行
- /// </summary>
- /// <returns></returns>
- public static bool IsAdministrator()
- {
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {
- // 获取当前用户的 Windows 身份
- WindowsIdentity identity = WindowsIdentity.GetCurrent();
- // 创建一个 WindowsPrincipal 对象,用于表示当前用户的主体
- WindowsPrincipal principal = new WindowsPrincipal(identity);
- // 检查当前用户是否属于管理员组
- return principal.IsInRole(WindowsBuiltInRole.Administrator);
- }
- return false;
- }
- /// <summary>
- /// 根据域名在hosts文件中找到对于的ip地址。
- /// </summary>
- /// <param name="domain"></param>
- /// <returns></returns>
- public static (string? ip, string msg) FindIpAddressForDomain(string domain)
- {
- string? lastMatchingIp = null;
- try
- {
- string filePath = @"C:\Windows\System32\drivers\etc\hosts";
- string[] lines = File.ReadAllLines(filePath);
- foreach (string line in lines)
- {
- string trimmedLine = line.Trim();
- if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("#"))
- {
- continue;
- }
- string[] parts = trimmedLine.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
- if (parts.Length >= 2)
- {
- string ip = parts[0];
- for (int i = 1; i < parts.Length; i++)
- {
- if (parts[i].Equals(domain, StringComparison.OrdinalIgnoreCase))
- {
- lastMatchingIp = ip;
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- return (null, $"读取文件时出错: {ex.Message}");
- }
- return (lastMatchingIp, "匹配结果");
- }
- /// <summary>
- /// 检查证书是否安装,切是否过期,true 已经安装,false 未安装,用于检查证书是否需要重新安装,最终返回 true不用安装。
- /// 代码中使用的是 CurrentUser 存储位置,如果你需要检查计算机级别的证书存储区,可以将 StoreLocation.CurrentUser 替换为 StoreLocation.LocalMachine,但这可能需要管理员权限。
- /// </summary>
- /// <param name="certificate"></param>
- /// <returns></returns>
- public static bool CheckCertificate(string certificatePath)
- {
- bool installed = false, expired=false;
- X509Certificate2 certificate = new X509Certificate2(certificatePath);
- // 定义要检查的证书存储区
- StoreName[] storeNames = { StoreName.Root, StoreName.CertificateAuthority, StoreName.My };
- foreach (StoreName storeName in storeNames)
- {
- if (IsAdministrator())
- {
- using (X509Store store = new X509Store(storeName, StoreLocation.LocalMachine))
- {
- try
- {
- // 打开存储区
- store.Open(OpenFlags.ReadOnly);
- // 查找匹配的证书
- X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
- if (collection.Count > 0)
- {
- installed = true;
- var certificateInstalled = collection.First();
- expired = CheckCertificateExpired(certificateInstalled);
- break;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"访问 {storeName} 存储区时出错: {ex.Message}");
- }
- finally
- {
- // 关闭存储区
- store.Close();
- }
- }
- }
- else {
- using (X509Store store = new X509Store(storeName, StoreLocation.CurrentUser))
- {
- try
- {
- // 打开存储区
- store.Open(OpenFlags.ReadOnly);
- // 查找匹配的证书
- X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
- if (collection.Count > 0)
- {
- installed = true;
- var certificateInstalled = collection.First();
- expired = CheckCertificateExpired(certificateInstalled);
- break;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"访问 {storeName} 存储区时出错: {ex.Message}");
- }
- finally
- {
- // 关闭存储区
- store.Close();
- }
- }
- }
-
- }
- return installed && !expired;
- }
- /// <summary>
- /// 检查证书是否过期,true 过期,false 未过期
- /// </summary>
- /// <param name="certificate"></param>
- /// <returns></returns>
- public static bool CheckCertificateExpired(X509Certificate2 certificate)
- {
- DateTime now = DateTime.Now;
- return now < certificate.NotBefore || now > certificate.NotAfter;
- }
- }
- }
|