diff --git a/src/WorkFlowCheck.MAUI/Handlers/JwtAuthorizationHandler.cs b/src/WorkFlowCheck.MAUI/Handlers/JwtAuthorizationHandler.cs new file mode 100644 index 0000000..82094fa --- /dev/null +++ b/src/WorkFlowCheck.MAUI/Handlers/JwtAuthorizationHandler.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace WorkFlowCheck.MAUI.Handlers +{ + public class JwtAuthorizationHandler : DelegatingHandler + { + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var token = await SecureStorage.GetAsync("AuthToken"); + + if (!string.IsNullOrEmpty(token)) + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); + } + + return await base.SendAsync(request, cancellationToken); + } + } +} diff --git a/src/WorkFlowCheck.MAUI/MainPage.xaml.cs b/src/WorkFlowCheck.MAUI/MainPage.xaml.cs index 3ac822a..4e37a64 100644 --- a/src/WorkFlowCheck.MAUI/MainPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/MainPage.xaml.cs @@ -57,6 +57,7 @@ namespace WorkFlowCheck.MAUI private void OnButtonLogout(object sender, EventArgs e) { SecureStorage.Default.Remove("WFCUser"); + SecureStorage.Default.Remove("AuthToken"); Shell.Current.GoToAsync("//LoginPage"); } private void OnButtonSync(object sender, EventArgs e) diff --git a/src/WorkFlowCheck.MAUI/MauiProgram.cs b/src/WorkFlowCheck.MAUI/MauiProgram.cs index a641352..320f345 100644 --- a/src/WorkFlowCheck.MAUI/MauiProgram.cs +++ b/src/WorkFlowCheck.MAUI/MauiProgram.cs @@ -11,6 +11,7 @@ using DevExpress.Maui; using Microsoft.Maui.Controls.Hosting; using Microsoft.Maui.Hosting; using CommunityToolkit.Maui.Core; +using WorkFlowCheck.MAUI.Handlers; namespace WorkFlowCheck.MAUI { @@ -35,12 +36,9 @@ namespace WorkFlowCheck.MAUI var configuration = new ConfigurationBuilder().SetBasePath(FileSystem.AppDataDirectory) // A konfigurációs fájl betöltése az alkalmazás adat könyvtárából .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); - builder.Services.AddHttpClient(client => - { - var apiUrl = configuration["ApiBaseUrl"]; // API-cím betöltése a konfigurációból - client.BaseAddress = new Uri(apiUrl); - }); + + builder.Services.AddHttpClient(); builder.Services.AddDbContext(); builder.Services.AddSingleton(configuration); @@ -64,9 +62,9 @@ namespace WorkFlowCheck.MAUI #if DEBUG builder.Logging.AddDebug(); #endif - // HttpClient regisztrálása az alap API-címmel + var app = builder.Build(); - // Szolgáltató tárolása globálisan + ServiceProvider = app.Services; return app; } diff --git a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs index 53a535c..6db577a 100644 --- a/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs +++ b/src/WorkFlowCheck.MAUI/Pages/Security/LoginPage.xaml.cs @@ -27,6 +27,7 @@ public partial class LoginPage : ContentPage var wFCUser = Newtonsoft.Json.JsonConvert.SerializeObject(response.Data); await SecureStorage.Default.SetAsync("WFCUser", "wFCUser"); + await SecureStorage.Default.SetAsync("AuthToken", response.Data.JwtToken); await Shell.Current.GoToAsync("//MainPage"); } diff --git a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs index a4d463b..66b2507 100644 --- a/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs +++ b/src/WorkFlowCheck.MAUI/Platforms/Android/MainActivity.cs @@ -12,6 +12,7 @@ namespace WorkFlowCheck.MAUI protected async override void OnDestroy() { SecureStorage.Default.Remove("WFCUser"); + SecureStorage.Default.Remove("AuthToken"); base.OnDestroy(); } protected override void OnCreate(Bundle savedInstanceState) diff --git a/src/WorkFlowCheck.MAUI/Services/BaseService.cs b/src/WorkFlowCheck.MAUI/Services/BaseService.cs index 9a94a00..066db87 100644 --- a/src/WorkFlowCheck.MAUI/Services/BaseService.cs +++ b/src/WorkFlowCheck.MAUI/Services/BaseService.cs @@ -1,8 +1,11 @@ using AutoMapper; +using DevExpress.XtraPrinting.Native; using Microsoft.Extensions.Configuration; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using WorkFlowCheck.MAUI.DataLayer; @@ -31,5 +34,14 @@ namespace WorkFlowCheck.MAUI.Services var apiKey = configuration["ApiKey"]; _httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey); } + protected async Task PrepareAuthenticatedRequestAsync() + { + var jwtToken = await SecureStorage.GetAsync("AuthToken"); + + if (!string.IsNullOrEmpty(jwtToken) && _httpClient != null) + { + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); + } + } } } diff --git a/src/WorkFlowCheck.MAUI/Services/CheckListService.cs b/src/WorkFlowCheck.MAUI/Services/CheckListService.cs index 128b97b..df55705 100644 --- a/src/WorkFlowCheck.MAUI/Services/CheckListService.cs +++ b/src/WorkFlowCheck.MAUI/Services/CheckListService.cs @@ -37,6 +37,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/checklist/startnew"; diff --git a/src/WorkFlowCheck.MAUI/Services/SyncService.cs b/src/WorkFlowCheck.MAUI/Services/SyncService.cs index e4b0566..36208da 100644 --- a/src/WorkFlowCheck.MAUI/Services/SyncService.cs +++ b/src/WorkFlowCheck.MAUI/Services/SyncService.cs @@ -27,6 +27,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints"; @@ -56,6 +57,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations"; @@ -85,6 +87,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments"; @@ -114,6 +117,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListTemplateHeader"; @@ -139,6 +143,5 @@ namespace WorkFlowCheck.MAUI.Services } } - } } diff --git a/src/WorkFlowCheck.MAUI/Services/UserService.cs b/src/WorkFlowCheck.MAUI/Services/UserService.cs index 63a6521..3f7258b 100644 --- a/src/WorkFlowCheck.MAUI/Services/UserService.cs +++ b/src/WorkFlowCheck.MAUI/Services/UserService.cs @@ -26,6 +26,7 @@ namespace WorkFlowCheck.MAUI.Services { try { + await PrepareAuthenticatedRequestAsync(); // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/User/1";