Services, service registration and dependency injection for auth

This commit is contained in:
Veljko Tosic
2026-02-12 19:49:37 +01:00
parent 95d5f9da87
commit c7e7735075
9 changed files with 160 additions and 20 deletions

View File

@@ -0,0 +1,35 @@
using System.Security.Claims;
using AipsCore.Application.Abstract.UserContext;
using AipsCore.Domain.Models.User.ValueObjects;
using Microsoft.AspNetCore.Http;
namespace AipsCore.Infrastructure.Persistence.Authentication;
public class HttpUserContext : IUserContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpUserContext(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public UserId GetCurrentUserId()
{
var user = _httpContextAccessor.HttpContext?.User;
if (user is null || !user.Identity!.IsAuthenticated)
{
throw new UnauthorizedAccessException("User is not authenticated");
}
var userIdClaim = user.FindFirst(ClaimTypes.NameIdentifier);
if (userIdClaim is null)
{
throw new UnauthorizedAccessException("User id claim not found");
}
return new UserId(userIdClaim.Value);
}
}