42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System.Management;
|
|
using App.Core.Interfaces.Licensing;
|
|
using App.Core.Services.Licensing;
|
|
|
|
namespace App.Infrastructure.Licensing;
|
|
|
|
/// <summary>
|
|
/// Lit l'UUID de la carte mère et le numéro de série du disque système via WMI, tels que
|
|
/// requis par le format de licence (voir docs/DATABASE_SCHEMA.md §Licence).
|
|
/// </summary>
|
|
public class WmiHardwareIdProvider : IHardwareIdProvider
|
|
{
|
|
public HardwareId GetCurrent()
|
|
{
|
|
var motherboardUuid = QuerySingleProperty("SELECT UUID FROM Win32_ComputerSystemProduct", "UUID")
|
|
?? throw new InvalidOperationException("Impossible de lire Win32_ComputerSystemProduct.UUID via WMI.");
|
|
|
|
// Win32_DiskDrive.Index=0 : le disque système (celui sur lequel Windows est installé),
|
|
// conformément au cahier des charges ("disque système index 0").
|
|
var diskSerial = QuerySingleProperty("SELECT SerialNumber FROM Win32_DiskDrive WHERE Index = 0", "SerialNumber")
|
|
?? throw new InvalidOperationException("Impossible de lire Win32_DiskDrive (Index=0).SerialNumber via WMI.");
|
|
|
|
return new HardwareId(motherboardUuid.Trim(), diskSerial.Trim());
|
|
}
|
|
|
|
private static string? QuerySingleProperty(string query, string propertyName)
|
|
{
|
|
using var searcher = new ManagementObjectSearcher(query);
|
|
using var results = searcher.Get();
|
|
|
|
foreach (ManagementBaseObject item in results)
|
|
{
|
|
using (item)
|
|
{
|
|
return item[propertyName]?.ToString();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|