new SignInService

This commit is contained in:
jojo aquino 2025-01-12 12:09:02 +00:00
parent 4cafd88502
commit 3e7663bbbe
5 changed files with 67 additions and 22 deletions

View File

@ -0,0 +1,9 @@
namespace EnotaryoPH.Web.Common.Services
{
public interface ISignInService
{
Task SignInAsync(UserLogin userLogin);
Task SignOutAsync();
}
}

View File

@ -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<Claim>
{
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();
}
}

View File

@ -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; }
}
}

View File

@ -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<IActionResult> OnGetAsync() => Page();
public async Task<IActionResult> OnGetLogoutAsync()
{
await HttpContext.SignOutAsync();
await _signInService.SignOutAsync();
return RedirectToPage("/Login");
}
@ -49,21 +46,12 @@ namespace EnotaryoPH.Web.Pages
return Page();
}
var claims = new List<Claim>
{
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);

View File

@ -35,6 +35,7 @@ namespace EnotaryoPH.Web
BaseUrl = config.GetValue<string>("BaseUrl") ?? ""
});
builder.Services.AddTransient<ISignInService, SignInService>();
builder.Services.AddTransient<IPasswordService, PasswordService>();
builder.Services.AddTransient<ICurrentUserService, CurrentUserService>();
builder.Services.AddTransient<ICompreFaceClient>(provider =>