MAUI-ba is jól bele lett rakva a Bearer token, ha van.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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<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.AddSingleton<IConfiguration>(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;
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user