new SignInService
This commit is contained in:
parent
4cafd88502
commit
3e7663bbbe
@ -0,0 +1,9 @@
|
|||||||
|
namespace EnotaryoPH.Web.Common.Services
|
||||||
|
{
|
||||||
|
public interface ISignInService
|
||||||
|
{
|
||||||
|
Task SignInAsync(UserLogin userLogin);
|
||||||
|
|
||||||
|
Task SignOutAsync();
|
||||||
|
}
|
||||||
|
}
|
38
EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs
Normal file
38
EnotaryoPH/EnotaryoPH.Web/Common/Services/SignInService.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
9
EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs
Normal file
9
EnotaryoPH/EnotaryoPH.Web/Common/Services/UserLogin.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +1,28 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Security.Claims;
|
|
||||||
using EnotaryoPH.Data;
|
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;
|
||||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
|
|
||||||
namespace EnotaryoPH.Web.Pages
|
namespace EnotaryoPH.Web.Pages
|
||||||
{
|
{
|
||||||
public class LoginModel : PageModel
|
public class LoginModel : PageModel
|
||||||
{
|
{
|
||||||
private readonly NotaryoDBContext _notaryoDBContext;
|
private readonly NotaryoDBContext _notaryoDBContext;
|
||||||
|
private readonly ISignInService _signInService;
|
||||||
private readonly IPasswordService _passwordService;
|
private readonly IPasswordService _passwordService;
|
||||||
|
|
||||||
public LoginModel(IPasswordService passwordService, NotaryoDBContext notaryoDBContext)
|
public LoginModel(IPasswordService passwordService, NotaryoDBContext notaryoDBContext, ISignInService signInService)
|
||||||
{
|
{
|
||||||
_passwordService = passwordService;
|
_passwordService = passwordService;
|
||||||
_notaryoDBContext = notaryoDBContext;
|
_notaryoDBContext = notaryoDBContext;
|
||||||
|
_signInService = signInService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> OnGetAsync() => Page();
|
public async Task<IActionResult> OnGetAsync() => Page();
|
||||||
|
|
||||||
public async Task<IActionResult> OnGetLogoutAsync()
|
public async Task<IActionResult> OnGetLogoutAsync()
|
||||||
{
|
{
|
||||||
await HttpContext.SignOutAsync();
|
await _signInService.SignOutAsync();
|
||||||
return RedirectToPage("/Login");
|
return RedirectToPage("/Login");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,21 +46,12 @@ namespace EnotaryoPH.Web.Pages
|
|||||||
return Page();
|
return Page();
|
||||||
}
|
}
|
||||||
|
|
||||||
var claims = new List<Claim>
|
await _signInService.SignInAsync(new UserLogin
|
||||||
{
|
{
|
||||||
new(ClaimTypes.NameIdentifier, user.User_UID.ToString()),
|
Email = user.Email,
|
||||||
new(ClaimTypes.Name, user.Email),
|
Role = user.Role,
|
||||||
new(ClaimTypes.Email, user.Email),
|
User_UID = user.User_UID.Value
|
||||||
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);
|
|
||||||
|
|
||||||
var returnUrl = Request.Query["ReturnUrl"].ToString() ?? string.Empty;
|
var returnUrl = Request.Query["ReturnUrl"].ToString() ?? string.Empty;
|
||||||
return RedirectToPage(string.IsNullOrEmpty(returnUrl) ? "/Principal/Dashboard/Dashboard" : returnUrl);
|
return RedirectToPage(string.IsNullOrEmpty(returnUrl) ? "/Principal/Dashboard/Dashboard" : returnUrl);
|
||||||
|
@ -35,6 +35,7 @@ namespace EnotaryoPH.Web
|
|||||||
BaseUrl = config.GetValue<string>("BaseUrl") ?? ""
|
BaseUrl = config.GetValue<string>("BaseUrl") ?? ""
|
||||||
});
|
});
|
||||||
|
|
||||||
|
builder.Services.AddTransient<ISignInService, SignInService>();
|
||||||
builder.Services.AddTransient<IPasswordService, PasswordService>();
|
builder.Services.AddTransient<IPasswordService, PasswordService>();
|
||||||
builder.Services.AddTransient<ICurrentUserService, CurrentUserService>();
|
builder.Services.AddTransient<ICurrentUserService, CurrentUserService>();
|
||||||
builder.Services.AddTransient<ICompreFaceClient>(provider =>
|
builder.Services.AddTransient<ICompreFaceClient>(provider =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user