20 lines
617 B
C#
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);
|
|
}
|
|
} |