feat(config): add Hikvision credentials factory and dev example settings
Resolves ISAPI credentials from device, env vars, or Development JSON. Logs load status at startup.main
parent
067dc77f37
commit
82dfa54e39
|
|
@ -10,6 +10,7 @@ public partial class App : Application
|
|||
AppLogger.Initialize();
|
||||
AppLogger.Info("[STARTUP] Application starting");
|
||||
AppLogger.Info("[STARTUP] Configuration loaded");
|
||||
HikvisionCredentialsFactory.LogDevelopmentCredentialStatus();
|
||||
DispatcherUnhandledException += (_, args) => AppLogger.Error("Unhandled UI exception.", args.Exception);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
using HikvisionAttendanceManager.App.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace HikvisionAttendanceManager.App.Services;
|
||||
|
||||
/// <summary>Resolves Hikvision ISAPI credentials: per-device, environment variables, then Development settings.</summary>
|
||||
public static class HikvisionCredentialsFactory
|
||||
{
|
||||
public const string DefaultUsernameVariable = "HIKVISION_MANAGER_DEFAULT_USERNAME";
|
||||
public const string DefaultPasswordVariable = "HIKVISION_MANAGER_DEFAULT_PASSWORD";
|
||||
|
||||
public static bool TryGetForDevice(Device device, out string username, out string password, out string error)
|
||||
{
|
||||
username = device.Username?.Trim() ?? "";
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
username = Environment.GetEnvironmentVariable(DefaultUsernameVariable)?.Trim() ?? "";
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
username = GetDevelopmentSetting("Hikvision:Username") ?? "";
|
||||
|
||||
password = !string.IsNullOrWhiteSpace(device.ProtectedPassword)
|
||||
? PasswordProtector.Unprotect(device.ProtectedPassword)
|
||||
: Environment.GetEnvironmentVariable(DefaultPasswordVariable) ?? "";
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
password = GetDevelopmentSetting("Hikvision:Password") ?? "";
|
||||
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
error = "Hikvision credentials are not configured for this device.";
|
||||
return false;
|
||||
}
|
||||
|
||||
error = "";
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool HasCredentials(Device device) => TryGetForDevice(device, out _, out _, out _);
|
||||
|
||||
public static void LogDevelopmentCredentialStatus()
|
||||
{
|
||||
if (!IsDevelopment())
|
||||
return;
|
||||
|
||||
var username = GetDevelopmentSetting("Hikvision:Username");
|
||||
var hasPassword = !string.IsNullOrWhiteSpace(GetDevelopmentSetting("Hikvision:Password"));
|
||||
if (!string.IsNullOrWhiteSpace(username) && hasPassword)
|
||||
AppLogger.Info($"Hikvision default credentials loaded for user '{username}'.");
|
||||
else
|
||||
AppLogger.Warning("Hikvision default credentials were not found in Development settings.");
|
||||
}
|
||||
|
||||
private static string? GetDevelopmentSetting(string key)
|
||||
{
|
||||
if (!IsDevelopment())
|
||||
return null;
|
||||
|
||||
return BuildDevelopmentConfiguration()[key]?.Trim();
|
||||
}
|
||||
|
||||
private static IConfiguration BuildDevelopmentConfiguration() =>
|
||||
new ConfigurationBuilder()
|
||||
.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", optional: true)
|
||||
.AddJsonFile("appsettings.Development.json", optional: true)
|
||||
.Build();
|
||||
|
||||
private static bool IsDevelopment()
|
||||
{
|
||||
var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(environment))
|
||||
return string.Equals(environment, "Development", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
#if DEBUG
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"Hrms": "Server=YOUR_SERVER;Port=3306;Database=hrms;User ID=YOUR_USER;Password=YOUR_PASSWORD;"
|
||||
},
|
||||
"Hikvision": {
|
||||
"Username": "admin",
|
||||
"Password": "Utopia01"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue