POST-os hívás tesztje az authentikációhoz

This commit is contained in:
2025-01-21 16:21:48 +01:00
parent 177aa668a6
commit 862f0a148b
8 changed files with 82 additions and 14 deletions
+2 -5
View File
@@ -9,11 +9,8 @@ namespace WorkFlowCheck.MAUI
{ {
InitializeComponent(); InitializeComponent();
//MainPage = new AppShell(); MainPage = serviceProvider.GetRequiredService<MainPage>();
var mainPage = new MainPage();
mainPage.Initialize(serviceProvider.GetRequiredService<IUserService>());
MainPage = mainPage;
} }
} }
} }
+5 -4
View File
@@ -7,14 +7,15 @@ namespace WorkFlowCheck.MAUI
int count = 0; int count = 0;
private IUserService _userService; private IUserService _userService;
public MainPage() public MainPage(IUserService userService)
{ {
InitializeComponent(); InitializeComponent();
}
public void Initialize(IUserService userService)
{
_userService = userService; _userService = userService;
} }
//public void Initialize(IUserService userService)
//{
// _userService = userService;
//}
private void OnCounterClicked(object sender, EventArgs e) private void OnCounterClicked(object sender, EventArgs e)
{ {
count++; count++;
+3 -1
View File
@@ -37,7 +37,9 @@ namespace WorkFlowCheck.MAUI
}); });
builder.Services.AddSingleton<IConfiguration>(configuration); builder.Services.AddSingleton<IConfiguration>(configuration);
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.AddTransient<MainPage>();
builder builder
.UseMauiApp<App>() .UseMauiApp<App>()
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WorkFlowCheck.MAUI.Pages.Security.LoginPage"
Title="LoginPage">
<StackLayout Padding="20" VerticalOptions="Center">
<Entry x:Name="UsernameEntry" Placeholder="Username" />
<Entry x:Name="PasswordEntry" Placeholder="Password" IsPassword="True" />
<Button Text="Log In" Clicked="OnLoginClicked" />
</StackLayout>
</ContentPage>
@@ -0,0 +1,30 @@
using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI.Pages.Security;
public partial class LoginPage : ContentPage
{
private readonly IUserService _userService;
public LoginPage(IUserService userService)
{
InitializeComponent();
_userService = userService;
}
private async void OnLoginClicked(object sender, EventArgs e)
{
// Példa bejelentkezés
bool success = _userService.Login(UsernameEntry.Text, PasswordEntry.Text);
if (success)
{
await DisplayAlert("Success", "You are now logged in.", "OK");
// Visszatérés a MainPage-re
await Navigation.PopAsync();
}
else
{
await DisplayAlert("Error", "Invalid login credentials.", "OK");
}
}
}
@@ -10,5 +10,6 @@ namespace WorkFlowCheck.MAUI.Services
public interface IUserService public interface IUserService
{ {
Task<ApiResponseDTO<UserDTO>> GetUserAsync(); Task<ApiResponseDTO<UserDTO>> GetUserAsync();
Task<IApiResponseDTO<UserDTO>> Login(string username, string password);
} }
} }
+24 -4
View File
@@ -20,10 +20,6 @@ namespace WorkFlowCheck.MAUI.Services
_httpClient.BaseAddress = new Uri(apiBaseUrl); _httpClient.BaseAddress = new Uri(apiBaseUrl);
var apiKey = configuration["ApiKey"]; var apiKey = configuration["ApiKey"];
// Alapértelmezett fejléc beállítása az API-kulccsal
//_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "YOUR_API_KEY");
// Ha egyéni fejlécet használsz:
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey); _httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
} }
public async Task<ApiResponseDTO<UserDTO>> GetUserAsync() public async Task<ApiResponseDTO<UserDTO>> GetUserAsync()
@@ -50,5 +46,29 @@ namespace WorkFlowCheck.MAUI.Services
}; };
} }
} }
public async Task<ApiResponseDTO<UserDTO>> Login(string username, string password)
{
try
{
// Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/User/Login";
// HTTP GET kérés küldése
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
return response ?? new ApiResponseDTO<UserDTO>
{
IsSuccess = false,
};
}
catch (Exception ex)
{
// Hiba visszaadása
return new ApiResponseDTO<UserDTO>
{
IsSuccess = false
};
}
}
} }
} }
@@ -71,4 +71,10 @@
<ProjectReference Include="..\WorkFlowCheck.Common\WorkFlowCheck.Common.csproj" /> <ProjectReference Include="..\WorkFlowCheck.Common\WorkFlowCheck.Common.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<MauiXaml Update="Pages\Security\LoginPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
</Project> </Project>