new CurrentUserService
This commit is contained in:
parent
65cbfc53a7
commit
2457a64c9c
@ -0,0 +1,28 @@
|
|||||||
|
using System.Security.Claims;
|
||||||
|
using System.Security.Principal;
|
||||||
|
|
||||||
|
namespace EnotaryoPH.Web.Common.Services
|
||||||
|
{
|
||||||
|
public class CurrentUserService : ICurrentUserService
|
||||||
|
{
|
||||||
|
private readonly ClaimsIdentity _user;
|
||||||
|
|
||||||
|
public CurrentUserService(IPrincipal principal) => _user = principal.Identity as ClaimsIdentity;
|
||||||
|
|
||||||
|
public string? GetEmail() => _user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
|
||||||
|
|
||||||
|
public Guid GetUser_UID()
|
||||||
|
{
|
||||||
|
if (_user == null)
|
||||||
|
{
|
||||||
|
return Guid.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var uid = _user.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||||
|
Guid.TryParse(uid, out var user_UID);
|
||||||
|
return user_UID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsAuthenticated() => _user?.IsAuthenticated ?? false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
namespace EnotaryoPH.Web.Common.Services
|
||||||
|
{
|
||||||
|
public interface ICurrentUserService
|
||||||
|
{
|
||||||
|
string? GetEmail();
|
||||||
|
Guid GetUser_UID();
|
||||||
|
bool IsAuthenticated();
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
using System.Security.Principal;
|
||||||
using EnotaryoPH.Data;
|
using EnotaryoPH.Data;
|
||||||
using EnotaryoPH.Web.Common.Services;
|
using EnotaryoPH.Web.Common.Services;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
@ -17,9 +18,12 @@ namespace EnotaryoPH.Web
|
|||||||
builder.Services.AddAuthorization(options => options.AddPolicy("PrincipalPolicy", policy => policy.RequireRole("Principal")));
|
builder.Services.AddAuthorization(options => options.AddPolicy("PrincipalPolicy", policy => policy.RequireRole("Principal")));
|
||||||
builder.Services.AddRazorPages(options => options.Conventions.AuthorizeFolder("/Principal", "PrincipalPolicy"));
|
builder.Services.AddRazorPages(options => options.Conventions.AuthorizeFolder("/Principal", "PrincipalPolicy"));
|
||||||
builder.Services.AddDbContext<NotaryoDBContext>();
|
builder.Services.AddDbContext<NotaryoDBContext>();
|
||||||
builder.Services.AddTransient<IPasswordService, PasswordService>();
|
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
builder.Services.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(120));
|
builder.Services.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(120));
|
||||||
|
builder.Services.AddTransient<IPrincipal>(provider => provider.GetService<IHttpContextAccessor>()?.HttpContext?.User);
|
||||||
|
|
||||||
|
builder.Services.AddTransient<IPasswordService, PasswordService>();
|
||||||
|
builder.Services.AddTransient<ICurrentUserService, CurrentUserService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user