From 3e7663bbbe74b2f8cdb91ab536fad62cb52d9a99 Mon Sep 17 00:00:00 2001 From: jojo aquino Date: Sun, 12 Jan 2025 12:09:02 +0000 Subject: [PATCH] new SignInService --- .../Common/Services/ISignInService.cs | 9 +++++ .../Common/Services/SignInService.cs | 38 +++++++++++++++++++ .../Common/Services/UserLogin.cs | 9 +++++ .../EnotaryoPH.Web/Pages/Login.cshtml.cs | 32 +++++----------- EnotaryoPH/EnotaryoPH.Web/Program.cs | 1 + 5 files changed, 67 insertions(+), 22 deletions(-) create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Services/ISignInService.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Services/ISignInService.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Services/ISignInService.cs new file mode 100644 index 0000000..26dcea3 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Services/ISignInService.cs @@ -0,0 +1,9 @@ +namespace EnotaryoPH.Web.Common.Services +{ + public interface ISignInService + { + Task SignInAsync(UserLogin userLogin); + + Task SignOutAsync(); + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs new file mode 100644 index 0000000..af2a8b6 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs @@ -0,0 +1,38 @@ +using System.Security.Claims; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; + +namespace EnotaryoPH.Web.Common.Services +{ + public class SignInService : ISignInService + { + private readonly IHttpContextAccessor _httpContextAccessor; + + public SignInService(IHttpContextAccessor httpContextAccessor) => _httpContextAccessor = httpContextAccessor; + + public async Task SignInAsync(UserLogin userLogin) + { + ArgumentException.ThrowIfNullOrWhiteSpace(userLogin.Email); + ArgumentException.ThrowIfNullOrWhiteSpace(userLogin.Role); + ArgumentOutOfRangeException.ThrowIfEqual(Guid.Empty, userLogin.User_UID); + + var claims = new List + { + new(ClaimTypes.NameIdentifier, userLogin.User_UID.ToString()), + new(ClaimTypes.Name, userLogin.Email), + new(ClaimTypes.Email, userLogin.Email), + new(ClaimTypes.Role, userLogin.Role!) + }; + var claimsIdentity = new ClaimsIdentity( + claims, CookieAuthenticationDefaults.AuthenticationScheme); + var authProperties = new AuthenticationProperties(); + + await _httpContextAccessor.HttpContext.SignInAsync( + CookieAuthenticationDefaults.AuthenticationScheme, + new ClaimsPrincipal(claimsIdentity), + authProperties); + } + + public async Task SignOutAsync() => await _httpContextAccessor.HttpContext.SignOutAsync(); + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs new file mode 100644 index 0000000..ad6dea3 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs @@ -0,0 +1,9 @@ +namespace EnotaryoPH.Web.Common.Services +{ + public class UserLogin + { + public string Email { get; set; } + public string Role { get; set; } + public Guid User_UID { get; set; } + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Pages/Login.cshtml.cs b/EnotaryoPH/EnotaryoPH.Web/Pages/Login.cshtml.cs index 076c0d7..c6cc2b7 100644 --- a/EnotaryoPH/EnotaryoPH.Web/Pages/Login.cshtml.cs +++ b/EnotaryoPH/EnotaryoPH.Web/Pages/Login.cshtml.cs @@ -1,31 +1,28 @@ using System.ComponentModel.DataAnnotations; -using System.Security.Claims; using EnotaryoPH.Data; -using EnotaryoPH.Web.Common.Services; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.EntityFrameworkCore; namespace EnotaryoPH.Web.Pages { public class LoginModel : PageModel { private readonly NotaryoDBContext _notaryoDBContext; + private readonly ISignInService _signInService; private readonly IPasswordService _passwordService; - public LoginModel(IPasswordService passwordService, NotaryoDBContext notaryoDBContext) + public LoginModel(IPasswordService passwordService, NotaryoDBContext notaryoDBContext, ISignInService signInService) { _passwordService = passwordService; _notaryoDBContext = notaryoDBContext; + _signInService = signInService; } public async Task OnGetAsync() => Page(); public async Task OnGetLogoutAsync() { - await HttpContext.SignOutAsync(); + await _signInService.SignOutAsync(); return RedirectToPage("/Login"); } @@ -49,21 +46,12 @@ namespace EnotaryoPH.Web.Pages return Page(); } - var claims = new List - { - new(ClaimTypes.NameIdentifier, user.User_UID.ToString()), - new(ClaimTypes.Name, user.Email), - new(ClaimTypes.Email, user.Email), - new(ClaimTypes.Role, user.Role!) - }; - var claimsIdentity = new ClaimsIdentity( - claims, CookieAuthenticationDefaults.AuthenticationScheme); - var authProperties = new AuthenticationProperties(); - - await HttpContext.SignInAsync( - CookieAuthenticationDefaults.AuthenticationScheme, - new ClaimsPrincipal(claimsIdentity), - authProperties); + await _signInService.SignInAsync(new UserLogin + { + Email = user.Email, + Role = user.Role, + User_UID = user.User_UID.Value + }); var returnUrl = Request.Query["ReturnUrl"].ToString() ?? string.Empty; return RedirectToPage(string.IsNullOrEmpty(returnUrl) ? "/Principal/Dashboard/Dashboard" : returnUrl); diff --git a/EnotaryoPH/EnotaryoPH.Web/Program.cs b/EnotaryoPH/EnotaryoPH.Web/Program.cs index db64bf4..62799a1 100644 --- a/EnotaryoPH/EnotaryoPH.Web/Program.cs +++ b/EnotaryoPH/EnotaryoPH.Web/Program.cs @@ -35,6 +35,7 @@ namespace EnotaryoPH.Web BaseUrl = config.GetValue("BaseUrl") ?? "" }); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(provider =>