mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-11 10:32:09 +08:00
30 lines
678 B
C#
30 lines
678 B
C#
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace BuildingBlocks.Utils;
|
|
|
|
public interface ICurrentUserProvider
|
|
{
|
|
long? GetCurrentUserId();
|
|
}
|
|
|
|
public class CurrentUserProvider : ICurrentUserProvider
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
public CurrentUserProvider(IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_httpContextAccessor = httpContextAccessor;
|
|
}
|
|
|
|
|
|
public long? GetCurrentUserId()
|
|
{
|
|
var nameIdentifier = _httpContextAccessor?.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
long.TryParse(nameIdentifier, out var userId);
|
|
|
|
return userId;
|
|
}
|
|
}
|