survey-webapp/SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs
shept 7a1078457d added UserContext for easy UserId getting
started writing custom services to implement users logic
2025-04-20 15:21:34 +05:00

19 lines
No EOL
567 B
C#

using System.Security.Claims;
using SurveyBackend.Core.Contexts;
namespace SurveyBackend.Contexts;
public class UserContext : IUserContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public int UserId =>
int.Parse(
_httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)
?.Value ?? throw new UnauthorizedAccessException());
}