Добавьте файлы проекта.
This commit is contained in:
20
PlaylistShared.Api/Extensions/ClaimsPrincipalExtensions.cs
Normal file
20
PlaylistShared.Api/Extensions/ClaimsPrincipalExtensions.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user