added UserContext for easy UserId getting

started writing custom services to implement users logic
This commit is contained in:
Вячеслав 2025-04-20 15:21:34 +05:00
parent d810101033
commit 7a1078457d
6 changed files with 83 additions and 6 deletions

View file

@ -0,0 +1,19 @@
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());
}