mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
Add CurrentUserSessionService for session ID retrieval
Introduces ICurrentUserSessionService and its implementation to provide access to the current user's session ID via the 'sid' claim. Registers the service in DI and adds comprehensive unit tests to validate correct and error scenarios.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Govor.Application.Infrastructure.Extensions;
|
||||
|
||||
public class CurrentUserSessionService : ICurrentUserSessionService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public CurrentUserSessionService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public Guid GetUserSessionId()
|
||||
{
|
||||
var user = _httpContextAccessor.HttpContext?.User;
|
||||
var userIdClaim = user?.FindFirst("sid")?.Value;
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
||||
{
|
||||
throw new UnauthorizedAccessException("Session id (sid) claim is missing or invalid");
|
||||
}
|
||||
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user