MAUI-ba is jól bele lett rakva a Bearer token, ha van.

This commit is contained in:
2025-03-14 12:19:16 +01:00
parent 50b14e8122
commit a72c9b342e
9 changed files with 50 additions and 8 deletions
@@ -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<HttpResponseMessage> 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);
}
}
}
+1
View File
@@ -57,6 +57,7 @@ namespace WorkFlowCheck.MAUI
private void OnButtonLogout(object sender, EventArgs e) private void OnButtonLogout(object sender, EventArgs e)
{ {
SecureStorage.Default.Remove("WFCUser"); SecureStorage.Default.Remove("WFCUser");
SecureStorage.Default.Remove("AuthToken");
Shell.Current.GoToAsync("//LoginPage"); Shell.Current.GoToAsync("//LoginPage");
} }
private void OnButtonSync(object sender, EventArgs e) private void OnButtonSync(object sender, EventArgs e)
+5 -7
View File
@@ -11,6 +11,7 @@ using DevExpress.Maui;
using Microsoft.Maui.Controls.Hosting; using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Hosting; using Microsoft.Maui.Hosting;
using CommunityToolkit.Maui.Core; using CommunityToolkit.Maui.Core;
using WorkFlowCheck.MAUI.Handlers;
namespace WorkFlowCheck.MAUI 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 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(); .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
builder.Services.AddHttpClient<UserService>(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<AppDbContext>(); builder.Services.AddDbContext<AppDbContext>();
builder.Services.AddSingleton<IConfiguration>(configuration); builder.Services.AddSingleton<IConfiguration>(configuration);
@@ -64,9 +62,9 @@ namespace WorkFlowCheck.MAUI
#if DEBUG #if DEBUG
builder.Logging.AddDebug(); builder.Logging.AddDebug();
#endif #endif
// HttpClient regisztrálása az alap API-címmel
var app = builder.Build(); var app = builder.Build();
// Szolgáltató tárolása globálisan
ServiceProvider = app.Services; ServiceProvider = app.Services;
return app; return app;
} }
@@ -27,6 +27,7 @@ public partial class LoginPage : ContentPage
var wFCUser = Newtonsoft.Json.JsonConvert.SerializeObject(response.Data); var wFCUser = Newtonsoft.Json.JsonConvert.SerializeObject(response.Data);
await SecureStorage.Default.SetAsync("WFCUser", "wFCUser"); await SecureStorage.Default.SetAsync("WFCUser", "wFCUser");
await SecureStorage.Default.SetAsync("AuthToken", response.Data.JwtToken);
await Shell.Current.GoToAsync("//MainPage"); await Shell.Current.GoToAsync("//MainPage");
} }
@@ -12,6 +12,7 @@ namespace WorkFlowCheck.MAUI
protected async override void OnDestroy() protected async override void OnDestroy()
{ {
SecureStorage.Default.Remove("WFCUser"); SecureStorage.Default.Remove("WFCUser");
SecureStorage.Default.Remove("AuthToken");
base.OnDestroy(); base.OnDestroy();
} }
protected override void OnCreate(Bundle savedInstanceState) protected override void OnCreate(Bundle savedInstanceState)
@@ -1,8 +1,11 @@
using AutoMapper; using AutoMapper;
using DevExpress.XtraPrinting.Native;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WorkFlowCheck.MAUI.DataLayer; using WorkFlowCheck.MAUI.DataLayer;
@@ -31,5 +34,14 @@ namespace WorkFlowCheck.MAUI.Services
var apiKey = configuration["ApiKey"]; var apiKey = configuration["ApiKey"];
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", 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);
}
}
} }
} }
@@ -37,6 +37,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/checklist/startnew"; string endpoint = $"{_httpClient.BaseAddress}api/checklist/startnew";
@@ -27,6 +27,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints"; string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
@@ -56,6 +57,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations"; string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllLocations";
@@ -85,6 +87,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments"; string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllEquipments";
@@ -114,6 +117,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListTemplateHeader"; string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckListTemplateHeader";
@@ -139,6 +143,5 @@ namespace WorkFlowCheck.MAUI.Services
} }
} }
} }
} }
@@ -26,6 +26,7 @@ namespace WorkFlowCheck.MAUI.Services
{ {
try try
{ {
await PrepareAuthenticatedRequestAsync();
// Az API végpont meghatározása // Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/User/1"; string endpoint = $"{_httpClient.BaseAddress}api/User/1";