Login fejlesztése

This commit is contained in:
2025-03-12 11:57:16 +01:00
parent c9ffad0d91
commit 439d8cafd5
3 changed files with 73 additions and 1 deletions
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text.Json;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
@@ -21,5 +23,28 @@ namespace WorkFlowCheck.Web.Pages.Account
public void OnGet()
{
}
public async Task<IActionResult> OnPostAsync()
{
var response = await _userService.Authenticate(UserDTO.UserName, UserDTO.Password);
if (response.IsSuccess)
{
var token = response.Data.JwtToken;
Response.Cookies.Append("AuthToken", token, new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.UtcNow.AddMinutes(30)
});
return RedirectToPage("/Index"); // vagy ahová szeretnéd
}
ErrorMessage = "Hibás bejelentkezés!";
return Page();
}
}
}
@@ -1,6 +1,9 @@
namespace WorkFlowCheck.Web.Services.Interfaces
using WorkFlowCheck.Common.DTO;
namespace WorkFlowCheck.Web.Services.Interfaces
{
public interface IUserService
{
Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password);
}
}
@@ -1,4 +1,6 @@
using Newtonsoft.Json;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Services
@@ -8,5 +10,47 @@ namespace WorkFlowCheck.Web.Services
public UserService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration)
{
}
public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
{
try
{
// Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/user/authenticate";
var userDTO = new UserDTO()
{
Email = "",
FirstName = "",
LastName = "",
Id = 0,
UserName = username,
Password = password,
RoleDTO = new List<RoleDTO>(),
};
// HTTP POST kérés küldése
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userDTO))
{
httpResponseMessage.EnsureSuccessStatusCode();
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<ApiResponseDTO<UserDTO>>(jsonString);
return response ?? new ApiResponseDTO<UserDTO>
{
IsSuccess = false,
};
}
}
catch (Exception ex)
{
// Hiba visszaadása
return new ApiResponseDTO<UserDTO>
{
IsSuccess = false
};
}
}
}
}