[Feature] Aggiunta protezione machine-binding tramite MachineGuard
- Nuovo progetto MachineGuard: libreria che verifica se la macchina corrente è autorizzata all'esecuzione tramite DPAPI (Data Protection API di Windows) - Nuovo progetto MachineGuardSetup: tool di configurazione da eseguire come Amministratore per registrare la macchina autorizzata - Data_Coupler.sln: aggiunti entrambi i nuovi progetti alla soluzione - Data_Coupler.csproj: aggiunto riferimento al progetto MachineGuard - Program.cs: integrazione MachineGuard all'avvio dell'applicazione; se la macchina non è autorizzata l'app viene arrestata immediatamente con log critico e scrittura nel Windows Event Log
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
<ProjectReference Include="..\DataConnection\DataConnection.csproj" />
|
||||
<ProjectReference Include="..\CredentialManager\CredentialManager.csproj" />
|
||||
<ProjectReference Include="..\Components\Components.csproj" />
|
||||
<ProjectReference Include="..\MachineGuard\MachineGuard.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -10,6 +10,7 @@ using CredentialManager;
|
||||
using Data_Coupler.Services;
|
||||
using Data_Coupler.BackgroundServices;
|
||||
using CredentialManager.Services;
|
||||
using MachineGuard;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -130,6 +131,9 @@ builder.Services.AddScoped<Data_Coupler.Services.IDeletionSyncService, Data_Coup
|
||||
// Register Background Services (solo uno per evitare duplicazioni)
|
||||
builder.Services.AddHostedService<Data_Coupler.BackgroundServices.ScheduledJobService>();
|
||||
|
||||
// Register MachineGuard — protezione machine-binding tramite DPAPI
|
||||
builder.Services.AddMachineGuard(builder.Configuration);
|
||||
|
||||
// Configurazione URL e timeout per servizio Windows
|
||||
var urls = builder.Configuration.GetValue<string>("Urls") ?? "http://*:7550";
|
||||
builder.WebHost.UseUrls(urls);
|
||||
@@ -143,6 +147,38 @@ builder.WebHost.ConfigureKestrel(serverOptions =>
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
#region MachineGuard — verifica autorizzazione macchina
|
||||
// Questa verifica deve avvenire PRIMA di qualsiasi altra inizializzazione.
|
||||
// Se la macchina non è autorizzata, l'applicazione viene arrestata immediatamente.
|
||||
{
|
||||
var machineGuard = app.Services.GetRequiredService<IMachineGuard>();
|
||||
if (!machineGuard.Verify())
|
||||
{
|
||||
var critLogger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||
critLogger.LogCritical(
|
||||
"MachineGuard: questa macchina NON è autorizzata a eseguire Data Coupler. " +
|
||||
"Eseguire MachineGuardSetup.exe come Amministratore per configurare questa macchina. " +
|
||||
"Applicazione arrestata.");
|
||||
|
||||
if (OperatingSystem.IsWindows())
|
||||
{
|
||||
try
|
||||
{
|
||||
using var eventLog = new System.Diagnostics.EventLog("Application");
|
||||
eventLog.Source = "DataCouplerService";
|
||||
eventLog.WriteEntry(
|
||||
"MachineGuard: macchina non autorizzata. " +
|
||||
"Eseguire MachineGuardSetup.exe come Amministratore. Applicazione arrestata.",
|
||||
System.Diagnostics.EventLogEntryType.Error);
|
||||
}
|
||||
catch { /* Ignora errori di scrittura EventLog */ }
|
||||
}
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
// Initialize database con timeout e retry
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user