30 lines
658 B
C#
30 lines
658 B
C#
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");
|
|
}
|
|
}
|
|
}
|