@page "/credentials" @using CredentialManager.Models @using DataConnection.CredentialManagement.Interfaces @using DataConnection.CredentialManagement.Models @using Microsoft.AspNetCore.Components.Forms @using Microsoft.JSInterop @inject IDataConnectionCredentialService CredentialService @inject IJSRuntime JSRuntime Gestione Credenziali

Gestione Credenziali

@if (loading) {
Caricamento...
} else if (!string.IsNullOrEmpty(errorMessage)) { } else {

Credenziali Database (@databaseCredentials.Count)

@if (databaseCredentials.Any()) {
@foreach (var credential in databaseCredentials) { }
Nome Tipo Database Host:Porta Database Username Azioni
@credential.Name @credential.DatabaseType @credential.Host:@credential.Port @if (string.IsNullOrEmpty(credential.DatabaseName)) { Connessione server } else { @credential.DatabaseName } @credential.Username
} else {
Nessuna credenziale database configurata.
}

Credenziali REST API / Servizi (@restApiCredentials.Count)

@if (restApiCredentials.Any()) {
@foreach (var credential in restApiCredentials) { }
Nome Tipo Servizio Base URL Autenticazione Timeout (s) Azioni
@credential.Name @if (credential.ServiceType == RestServiceType.SapB1ServiceLayer) { SAP B1 } else if (credential.ServiceType == RestServiceType.Salesforce) { Salesforce @if (credential.IsSandbox) { Sandbox } } else { Generic REST } @credential.BaseUrl @GetAuthenticationType(credential) @credential.TimeoutSeconds
} else {
Nessuna credenziale REST API configurata.
}
} @if (showDatabaseModal) { } @if (showRestApiModal) { } @code { private List databaseCredentials = new(); private List restApiCredentials = new(); private bool loading = true; private string? errorMessage = null; private bool testingConnection = false; // Modal state private bool showDatabaseModal = false; private bool showRestApiModal = false; private DatabaseCredential? editingDatabaseCredential = null; private RestApiCredential? editingRestApiCredential = null; private DatabaseCredential currentDatabaseCredential = new(); private RestApiCredential currentRestApiCredential = new(); protected override async Task OnInitializedAsync() { await RefreshCredentials(); } private async Task RefreshCredentials() { loading = true; errorMessage = null; try { databaseCredentials = await CredentialService.GetAllDatabaseCredentialsAsync(); restApiCredentials = await CredentialService.GetAllRestApiCredentialsAsync(); } catch (Exception ex) { errorMessage = $"Errore nel caricamento delle credenziali: {ex.Message}"; // Se l'errore è relativo alla tabella mancante, mostriamo un messaggio più specifico if (ex.Message.Contains("no such table: Credentials")) { errorMessage = "Database non inizializzato correttamente. Riavviare l'applicazione."; } } finally { loading = false; } } #region Database Credential Methods private void ShowAddDatabaseModal() { editingDatabaseCredential = null; currentDatabaseCredential = new DatabaseCredential { DatabaseType = CredentialManager.Models.DatabaseType.SqlServer, Port = 1433, CommandTimeout = 30 }; showDatabaseModal = true; } private void EditDatabaseCredential(DatabaseCredential credential) { editingDatabaseCredential = credential; currentDatabaseCredential = new DatabaseCredential { Name = credential.Name, DatabaseType = credential.DatabaseType, Host = credential.Host, Port = credential.Port, DatabaseName = credential.DatabaseName, Username = credential.Username, Password = credential.Password, CommandTimeout = credential.CommandTimeout, IgnoreSslErrors = credential.IgnoreSslErrors }; showDatabaseModal = true; } private async Task SaveDatabaseCredential() { try { await CredentialService.SaveDatabaseCredentialAsync(currentDatabaseCredential); await JSRuntime.InvokeVoidAsync("alert", "Credenziale database salvata con successo!"); CloseDatabaseModal(); await RefreshCredentials(); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel salvare la credenziale: {ex.Message}"); } } private void CloseDatabaseModal() { showDatabaseModal = false; editingDatabaseCredential = null; } private async Task TestDatabaseConnection(DatabaseCredential credential) { try { var (success, message) = await CredentialService.TestDatabaseConnectionAsync(credential.Name); var title = success ? "Test Connessione - Successo" : "Test Connessione - Errore"; await JSRuntime.InvokeVoidAsync("alert", $"{title}\n\n{message}"); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel test della connessione: {ex.Message}"); } } private async Task TestCurrentDatabaseConnection() { if (testingConnection) return; testingConnection = true; try { // Valida i campi obbligatori if (string.IsNullOrEmpty(currentDatabaseCredential.Name) || string.IsNullOrEmpty(currentDatabaseCredential.Host) || string.IsNullOrEmpty(currentDatabaseCredential.Username) || string.IsNullOrEmpty(currentDatabaseCredential.Password)) { await JSRuntime.InvokeVoidAsync("alert", "Compila tutti i campi obbligatori prima di testare la connessione."); return; } var (success, message) = await CredentialService.TestDatabaseConnectionAsync(currentDatabaseCredential); var title = success ? "Test Connessione - Successo" : "Test Connessione - Errore"; await JSRuntime.InvokeVoidAsync("alert", $"{title}\n\n{message}"); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel test della connessione: {ex.Message}"); } finally { testingConnection = false; } } #endregion #region REST API Credential Methods private void ShowAddRestApiModal() { editingRestApiCredential = null; currentRestApiCredential = new RestApiCredential { ServiceType = RestServiceType.Generic, TimeoutSeconds = 100 }; SetDefaultsForServiceType(currentRestApiCredential.ServiceType); showRestApiModal = true; } private void EditRestApiCredential(RestApiCredential credential) { editingRestApiCredential = credential; currentRestApiCredential = new RestApiCredential { Name = credential.Name, ServiceType = credential.ServiceType, BaseUrl = credential.BaseUrl, ApiKey = credential.ApiKey, Username = credential.Username, Password = credential.Password, AuthToken = credential.AuthToken, BearerToken = credential.BearerToken, TimeoutSeconds = credential.TimeoutSeconds, IgnoreSslErrors = credential.IgnoreSslErrors, Headers = credential.Headers, AdditionalParameters = credential.AdditionalParameters, // Campi SAP B1 CompanyDatabase = credential.CompanyDatabase, Language = credential.Language, Version = credential.Version, UseTrustedConnection = credential.UseTrustedConnection, // Campi Salesforce SecurityToken = credential.SecurityToken, ClientId = credential.ClientId, ClientSecret = credential.ClientSecret, ApiVersion = credential.ApiVersion, IsSandbox = credential.IsSandbox, UseSoapApi = credential.UseSoapApi, RefreshToken = credential.RefreshToken, AccessToken = credential.AccessToken, TokenExpiry = credential.TokenExpiry }; showRestApiModal = true; } private async Task SaveRestApiCredential() { try { await CredentialService.SaveRestApiCredentialAsync(currentRestApiCredential); await JSRuntime.InvokeVoidAsync("alert", $"Credenziale {GetServiceTypeDisplayName(currentRestApiCredential.ServiceType)} salvata con successo!"); CloseRestApiModal(); await RefreshCredentials(); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel salvare la credenziale: {ex.Message}"); } } private void CloseRestApiModal() { showRestApiModal = false; editingRestApiCredential = null; } private async Task TestRestApiConnection(RestApiCredential credential) { try { var (success, message) = credential.ServiceType switch { RestServiceType.SapB1ServiceLayer => await CredentialService.TestSapB1ConnectionAsync(credential.Name), RestServiceType.Salesforce => await CredentialService.TestSalesforceConnectionAsync(credential.Name), _ => await CredentialService.TestRestApiConnectionAsync(credential.Name) }; var title = success ? $"Test {GetServiceTypeDisplayName(credential.ServiceType)} - Successo" : $"Test {GetServiceTypeDisplayName(credential.ServiceType)} - Errore"; await JSRuntime.InvokeVoidAsync("alert", $"{title}\n\n{message}"); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel test della connessione: {ex.Message}"); } } private void OnServiceTypeChanged(ChangeEventArgs e) { if (Enum.TryParse(e.Value?.ToString(), out var serviceType)) { currentRestApiCredential.ServiceType = serviceType; SetDefaultsForServiceType(serviceType); } } private void SetDefaultsForServiceType(RestServiceType serviceType) { switch (serviceType) { case RestServiceType.SapB1ServiceLayer: currentRestApiCredential.Language = "en-US"; currentRestApiCredential.Version = "v1"; currentRestApiCredential.TimeoutSeconds = 300; if (string.IsNullOrEmpty(currentRestApiCredential.BaseUrl)) currentRestApiCredential.BaseUrl = "https://server:50000/b1s/v1/"; break; case RestServiceType.Salesforce: currentRestApiCredential.ApiVersion = "59.0"; currentRestApiCredential.TimeoutSeconds = 120; currentRestApiCredential.IsSandbox = false; currentRestApiCredential.UseSoapApi = false; if (string.IsNullOrEmpty(currentRestApiCredential.BaseUrl)) currentRestApiCredential.BaseUrl = "https://login.salesforce.com"; break; case RestServiceType.Generic: default: currentRestApiCredential.TimeoutSeconds = 100; break; } } private string GetServiceTypeDisplayName(RestServiceType serviceType) { return serviceType switch { RestServiceType.SapB1ServiceLayer => "SAP B1 Service Layer", RestServiceType.Salesforce => "Salesforce", RestServiceType.Generic => "REST API", _ => "REST API" }; } private string GetUrlFieldLabel(RestServiceType serviceType) { return serviceType switch { RestServiceType.SapB1ServiceLayer => "Server URL", RestServiceType.Salesforce => "Login URL", _ => "Base URL" }; } private string GetUrlPlaceholder(RestServiceType serviceType) { return serviceType switch { RestServiceType.SapB1ServiceLayer => "https://server:50000/b1s/v1/", RestServiceType.Salesforce => "https://login.salesforce.com", _ => "https://api.example.com" }; } private string GetUrlHelpText(RestServiceType serviceType) { return serviceType switch { RestServiceType.SapB1ServiceLayer => "URL del SAP B1 Service Layer (esempio: https://server:50000/b1s/v1/)", RestServiceType.Salesforce => "Production: https://login.salesforce.com | Sandbox: https://test.salesforce.com", _ => "URL base del servizio REST API" }; } private void OnSandboxChanged(ChangeEventArgs e) { if (bool.TryParse(e.Value?.ToString(), out bool isSandbox)) { currentRestApiCredential.IsSandbox = isSandbox; currentRestApiCredential.BaseUrl = isSandbox ? "https://test.salesforce.com" : "https://login.salesforce.com"; } } #endregion #region Common Methods private async Task DeleteCredential(string name, bool isDatabase) { if (await JSRuntime.InvokeAsync("confirm", $"Sei sicuro di voler eliminare la credenziale '{name}'?")) { try { bool success; if (isDatabase) success = await CredentialService.DeleteDatabaseCredentialAsync(name); else success = await CredentialService.DeleteRestApiCredentialAsync(name); if (success) { await JSRuntime.InvokeVoidAsync("alert", "Credenziale eliminata con successo!"); await RefreshCredentials(); } else { await JSRuntime.InvokeVoidAsync("alert", "Errore nell'eliminazione della credenziale."); } } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nell'eliminazione: {ex.Message}"); } } } private string GetAuthenticationType(RestApiCredential credential) { if (!string.IsNullOrEmpty(credential.ApiKey)) return "API Key"; if (!string.IsNullOrEmpty(credential.AuthToken)) return "Auth Token"; if (!string.IsNullOrEmpty(credential.Username)) return "Basic Auth"; return "Nessuna"; } private async Task TestRestApiConnectionFromModal() { try { testingConnection = true; // Creiamo una credenziale temporanea per il test var tempCredential = new RestApiCredential { Name = $"temp_test_{Guid.NewGuid():N}", ServiceType = currentRestApiCredential.ServiceType, BaseUrl = currentRestApiCredential.BaseUrl, Username = currentRestApiCredential.Username, Password = currentRestApiCredential.Password, ApiKey = currentRestApiCredential.ApiKey, AuthToken = currentRestApiCredential.AuthToken, TimeoutSeconds = currentRestApiCredential.TimeoutSeconds, IgnoreSslErrors = currentRestApiCredential.IgnoreSslErrors, // Campi SAP B1 CompanyDatabase = currentRestApiCredential.CompanyDatabase, Version = currentRestApiCredential.Version, Language = currentRestApiCredential.Language, UseTrustedConnection = currentRestApiCredential.UseTrustedConnection, // Campi Salesforce SecurityToken = currentRestApiCredential.SecurityToken, ClientId = currentRestApiCredential.ClientId, ClientSecret = currentRestApiCredential.ClientSecret, ApiVersion = currentRestApiCredential.ApiVersion, IsSandbox = currentRestApiCredential.IsSandbox, UseSoapApi = currentRestApiCredential.UseSoapApi }; // Salviamo temporaneamente la credenziale per il test await CredentialService.SaveRestApiCredentialAsync(tempCredential); // Testiamo la connessione var (success, message) = await CredentialService.TestRestApiConnectionAsync(tempCredential.Name); // Rimuoviamo la credenziale temporanea await CredentialService.DeleteRestApiCredentialAsync(tempCredential.Name); var title = success ? "Test Connessione - Successo" : "Test Connessione - Errore"; await JSRuntime.InvokeVoidAsync("alert", $"{title}\n\n{message}"); } catch (Exception ex) { await JSRuntime.InvokeVoidAsync("alert", $"Errore nel test della connessione: {ex.Message}"); } finally { testingConnection = false; } } private bool IsFieldRequired(string fieldName, RestServiceType serviceType) { return (fieldName, serviceType) switch { ("Username", RestServiceType.Generic) => string.IsNullOrEmpty(currentRestApiCredential.ApiKey) && string.IsNullOrEmpty(currentRestApiCredential.AuthToken), ("Password", RestServiceType.Generic) => string.IsNullOrEmpty(currentRestApiCredential.ApiKey) && string.IsNullOrEmpty(currentRestApiCredential.AuthToken), ("Username", RestServiceType.SapB1ServiceLayer) => !currentRestApiCredential.UseTrustedConnection, ("Password", RestServiceType.SapB1ServiceLayer) => !currentRestApiCredential.UseTrustedConnection, ("CompanyDatabase", RestServiceType.SapB1ServiceLayer) => true, ("Username", RestServiceType.Salesforce) => true, ("Password", RestServiceType.Salesforce) => true, ("SecurityToken", RestServiceType.Salesforce) => string.IsNullOrEmpty(currentRestApiCredential.ClientId) && string.IsNullOrEmpty(currentRestApiCredential.ClientSecret), _ => false }; } private string GetFieldLabel(string fieldName, RestServiceType serviceType) { var label = (fieldName, serviceType) switch { ("Username", RestServiceType.SapB1ServiceLayer) => "Username", ("Password", RestServiceType.SapB1ServiceLayer) => "Password", ("CompanyDatabase", RestServiceType.SapB1ServiceLayer) => "Company Database", ("Username", RestServiceType.Salesforce) => "Username", ("Password", RestServiceType.Salesforce) => "Password", ("SecurityToken", RestServiceType.Salesforce) => "Security Token", _ => fieldName }; return IsFieldRequired(fieldName, serviceType) ? $"{label} *" : label; } #endregion }