简单的权限管理
在Window系统中,关于权限管理的方案很多,今天我们说下 机器特征识别 + 权限验证
1、我写了一个exe执行文件,使用WPF写的,在我的电脑可以运行,在我的朋友电脑运行要安装其他的dll,后来我知道,我朋友不是专业做开发的,需要下载微软的一些库才行,他跟据电脑提示下载了,然后可以在他的电脑成功运行我的exe文件了,这件事给了我启发,我要做一个权限验证,虽然是同一个exe程序,但是在我的电脑上运行是管理员,在他的电脑上运行的是普通用户,假如我的WPF做了两个界面一个AdminView,一个CommonView,在我的电脑运行这个exe,打开的是AdminView界面,在其他任何人的电脑上打开都是CommonView界面。
// MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadViewBasedOnAuthorization();
}
private void LoadViewBasedOnAuthorization()
{
bool isAdmin = IsCurrentUserAdmin();
bool isMyComputer = CheckMachineFingerprint();
if (isMyComputer && isAdmin)
{
this.Content = new AdminView();
}
else
{
this.Content = new CommonView();
}
}
// 验证管理员权限
private bool IsCurrentUserAdmin()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
// 生成机器指纹(示例方案)
private bool CheckMachineFingerprint()
{
// 组合硬件特征(需提前在你的电脑生成并加密存储)
string cpuId = GetCpuId();
string biosId = GetBiosId();
string hash = CalculateHash(cpuId + biosId);
// 与预存指纹比对(此处用常量演示,实际应加密存储)
return hash == "A3F5D78E9C2B1A4F";
}
// 获取CPU序列号
private string GetCpuId()
{
using (ManagementObject mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'"))
{
return mo["ProcessorId"].ToString();
}
}
// 获取BIOS标识
private string GetBiosId()
{
using (ManagementObject mo = new ManagementObject("Win32_BIOS.Manufacturer='American Megatrends Inc.'"))
{
return mo["SerialNumber"].ToString();
}
}
// 计算SHA256哈希
private string CalculateHash(string input)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));
return BitConverter.ToString(bytes).Replace("-", "");
}
}
}
二、实现原理说明
- 机器指纹生成方案
硬件特征 获取方式 唯一性
CPU序列号 Win32_Processor.ProcessorId 高
主板序列号 Win32_BaseBoard.SerialNumber 高
硬盘序列号 Win32_DiskDrive.SerialNumber 高
MAC地址 Win32_NetworkAdapter.MACAddress 中
建议组合: CPU序列号 + 主板序列号 哈希后的值作为机器指纹
graph TD
A[启动程序] --> B{是否目标机器?}
B -- 是 --> C{是否管理员?}
B -- 否 --> D[显示CommonView]
C -- 是 --> E[显示AdminView]
C -- 否 --> D
指纹加密存储
// 使用AES加密存储指纹
public class FingerprintStorage
{
private static byte[] _key = Encoding.UTF8.GetBytes("YourSecretKey123"); // 实际应使用安全密钥
public static void SaveFingerprint(string hash)
{
using (Aes aes = Aes.Create())
{
aes.Key = _key;
byte[] encrypted = aes.Encrypt(Encoding.UTF8.GetBytes(hash));
File.WriteAllBytes("fingerprint.dat", encrypted);
}
}
public static string LoadFingerprint()
{
byte[] encrypted = File.ReadAllBytes("fingerprint.dat");
using (Aes aes = Aes.Create())
{
aes.Key = _key;
byte[] decrypted = aes.Decrypt(encrypted);
return Encoding.UTF8.GetString(decrypted);
}
}
}
数字签名验证
bool VerifyAuthenticodeSignature(string exePath)
{
X509Certificate2 cert = new X509Certificate2(exePath);
using (var chain = new X509Chain())
{
return chain.Build(cert) &&
cert.Subject.Contains("Your Company Name");
}
}
在自己电脑生成指纹
// 生成并保存指纹(仅运行一次)
string cpuId = GetCpuId();
string biosId = GetBiosId();
string hash = CalculateHash(cpuId + biosId);
FingerprintStorage.SaveFingerprint(hash);

浙公网安备 33010602011771号