First Commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace JobTrackerApi.Services;
|
||||
|
||||
public interface ICurrentUserService
|
||||
{
|
||||
string? UserId { get; }
|
||||
}
|
||||
|
||||
public sealed class CurrentUserService : ICurrentUserService
|
||||
{
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public CurrentUserService(IHttpContextAccessor http)
|
||||
{
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public string? UserId
|
||||
{
|
||||
get
|
||||
{
|
||||
var u = _http.HttpContext?.User;
|
||||
if (u is null) return null;
|
||||
if (u.Identity?.IsAuthenticated != true) return null;
|
||||
return u.FindFirstValue(ClaimTypes.NameIdentifier) ?? u.FindFirstValue("sub");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user