Files
PlaylistShared/PlaylistShared.Api/Extensions/ClaimsPrincipalExtensions.cs
2026-04-13 14:16:44 +03:00

20 lines
617 B
C#

using System.Security.Claims;
namespace PlaylistShared.Api.Extensions;
public static class ClaimsPrincipalExtensions
{
public static Guid GetUserId(this ClaimsPrincipal user)
{
var id = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(id))
throw new UnauthorizedAccessException("User ID not found");
return Guid.Parse(id);
}
public static Guid? GetUserIdOrNull(this ClaimsPrincipal user)
{
var id = user.FindFirst(ClaimTypes.NameIdentifier)?.Value;
return string.IsNullOrEmpty(id) ? null : Guid.Parse(id);
}
}