From 2457a64c9cb3ba82006a00956e14ba9691eb1ee9 Mon Sep 17 00:00:00 2001 From: jojo aquino Date: Sun, 15 Dec 2024 14:34:59 +0000 Subject: [PATCH] new CurrentUserService --- .../Common/Services/CurrentUserService.cs | 28 +++++++++++++++++++ .../Common/Services/ICurrentUserService.cs | 10 +++++++ EnotaryoPH/EnotaryoPH.Web/Program.cs | 6 +++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Services/CurrentUserService.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Services/ICurrentUserService.cs diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Services/CurrentUserService.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Services/CurrentUserService.cs new file mode 100644 index 0000000..29931a4 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Services/CurrentUserService.cs @@ -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; + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Services/ICurrentUserService.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Services/ICurrentUserService.cs new file mode 100644 index 0000000..dd4cf13 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Services/ICurrentUserService.cs @@ -0,0 +1,10 @@ + +namespace EnotaryoPH.Web.Common.Services +{ + public interface ICurrentUserService + { + string? GetEmail(); + Guid GetUser_UID(); + bool IsAuthenticated(); + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Program.cs b/EnotaryoPH/EnotaryoPH.Web/Program.cs index 9d4cd94..a678b87 100644 --- a/EnotaryoPH/EnotaryoPH.Web/Program.cs +++ b/EnotaryoPH/EnotaryoPH.Web/Program.cs @@ -1,3 +1,4 @@ +using System.Security.Principal; using EnotaryoPH.Data; using EnotaryoPH.Web.Common.Services; 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.AddRazorPages(options => options.Conventions.AuthorizeFolder("/Principal", "PrincipalPolicy")); builder.Services.AddDbContext(); - builder.Services.AddTransient(); builder.Services.AddHttpContextAccessor(); builder.Services.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(120)); + builder.Services.AddTransient(provider => provider.GetService()?.HttpContext?.User); + + builder.Services.AddTransient(); + builder.Services.AddTransient(); var app = builder.Build();