21 lines
623 B
C#
21 lines
623 B
C#
using System.Security.Claims;
|
|
|
|
namespace QMax.Api.Infrastructure.Auth;
|
|
|
|
public static class UserContext
|
|
{
|
|
public static Guid GetUserId(this ClaimsPrincipal principal)
|
|
{
|
|
var value = principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
|
?? principal.FindFirstValue("sub")
|
|
?? throw new InvalidOperationException("Authenticated user id is missing.");
|
|
return Guid.Parse(value);
|
|
}
|
|
|
|
public static Guid? GetSessionId(this ClaimsPrincipal principal)
|
|
{
|
|
var value = principal.FindFirstValue("sid");
|
|
return Guid.TryParse(value, out var id) ? id : null;
|
|
}
|
|
}
|