Na, majd otthon ...

This commit is contained in:
Iván Szabó
2025-01-06 12:49:34 +01:00
parent a80870b5c3
commit 92801fefb9
11 changed files with 106 additions and 18 deletions
@@ -15,6 +15,7 @@
if (!context.Request.Headers.TryGetValue(ApiKeyHeaderName, out var extractedApiKey))
{
context.Response.StatusCode = 401; // Unauthorized
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.WriteAsync("API Key hiányzik.");
return;
}
@@ -25,6 +26,7 @@
if (!apiKey.Equals(extractedApiKey))
{
context.Response.StatusCode = 403; // Forbidden
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.WriteAsync("Helytelen API Key.");
return;
}
@@ -6,7 +6,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "todos",
"applicationUrl": "http://localhost:5069",
"applicationUrl": "https://localhost:5069",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
+1 -1
View File
@@ -2,7 +2,7 @@
"DefaultConnection": {
"ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WorkFlowCheckDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False"
},
"ApiKey": "YOUR_SECRET_API_KEY",
"ApiKey": "RUJeLSpSMzVASUdaRCEzUyYxRSE0VyFISFRSJC0zRzhLM1hCSDU=",
"Logging": {
"LogLevel": {
"Default": "Information",
+10 -3
View File
@@ -1,12 +1,19 @@
namespace WorkFlowCheck.MAUI
using Microsoft.Extensions.DependencyInjection;
using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI
{
public partial class App : Application
{
public App()
public App(IServiceProvider serviceProvider)
{
InitializeComponent();
MainPage = new AppShell();
//MainPage = new AppShell();
var mainPage = new MainPage();
mainPage.Initialize(serviceProvider.GetRequiredService<IUserService>());
MainPage = mainPage;
}
}
}
+10 -3
View File
@@ -7,14 +7,15 @@
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
<!--<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
SemanticProperties.Description="dot net bot in a race car number eight" />-->
<Label
Text="Hello, World!"
x:Name="WelcomeLabel"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
@@ -24,9 +25,15 @@
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
<Button
x:Name="ButtonGetUser"
Text="Get user info"
SemanticProperties.Hint="Get user info"
Clicked="OnButtonGetUser"
HorizontalOptions="Fill" />
<Button
x:Name="CounterBtn"
Text="Click me"
Text="Hahó II."
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked"
HorizontalOptions="Fill" />
+25 -2
View File
@@ -1,14 +1,20 @@
namespace WorkFlowCheck.MAUI
using WorkFlowCheck.BL.DTO;
using WorkFlowCheck.MAUI.Services;
namespace WorkFlowCheck.MAUI
{
public partial class MainPage : ContentPage
{
int count = 0;
private IUserService _userService;
public MainPage()
{
InitializeComponent();
}
public void Initialize(IUserService userService)
{
_userService = userService;
}
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
@@ -20,6 +26,23 @@
SemanticScreenReader.Announce(CounterBtn.Text);
}
private async void OnButtonGetUser(object sender, EventArgs e)
{
var response = await _userService.GetUserAsync();
if (response != null)
{
try
{
WelcomeLabel.Text = response.Data.Name;
}
catch (Exception ex)
{
WelcomeLabel.Text = ex.Message;
}
}
}
}
}
+27 -2
View File
@@ -1,4 +1,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WorkFlowCheck.MAUI.Services;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.DependencyInjection;
namespace WorkFlowCheck.MAUI
{
@@ -7,6 +11,19 @@ namespace WorkFlowCheck.MAUI
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
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.AddSingleton<IConfiguration>(configuration);
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.AddSingleton<IUserService, UserService>();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
@@ -18,8 +35,16 @@ namespace WorkFlowCheck.MAUI
#if DEBUG
builder.Logging.AddDebug();
#endif
// HttpClient regisztrálása az alap API-címmel
return builder.Build();
var app = builder.Build();
// Szolgáltató tárolása globálisan
ServiceProvider = app.Services;
return app;
}
public static IServiceProvider ServiceProvider { get; private set; }
}
}
@@ -0,0 +1,4 @@
{
"ApiBaseUrl": "https://localhost:5069",
"ApiKey": "RUJeLSpSMzVASUdaRCEzUyYxRSE0VyFISFRSJC0zRzhLM1hCSDU="
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkFlowCheck.BL.DTO;
namespace WorkFlowCheck.MAUI.Services
{
public interface IUserService
{
Task<ApiResponseDTO<UserDTO>> GetUserAsync();
}
}
+10 -6
View File
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
@@ -8,24 +9,27 @@ using WorkFlowCheck.BL.DTO;
namespace WorkFlowCheck.MAUI.Services
{
public class UserService
public class UserService : IUserService
{
private readonly HttpClient _httpClient;
public UserService(HttpClient httpClient)
public UserService(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
_httpClient.BaseAddress = new Uri("https://192.168.50.128:5069");
// Alapértelmezett fejléc beállítása az API-kulccsal
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "YOUR_API_KEY");
//_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", "YOUR_API_KEY");
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", "RUJeLSpSMzVASUdaRCEzUyYxRSE0VyFISFRSJC0zRzhLM1hCSDU=");
}
public async Task<ApiResponseDTO<UserDTO>> GetUserAsync()
{
try
{
// Az API végpont meghatározása
string endpoint = "api/user";
string endpoint = $"{_httpClient.BaseAddress}api/User";
// HTTP GET kérés küldése
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
@@ -57,8 +57,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />