19 lines
584 B
C#
19 lines
584 B
C#
using System.Security.Claims;
|
|||
|
|
using Microsoft.IdentityModel.JsonWebTokens;
|
||
|
|
|
||
|
|
namespace Mws.Api;
|
||
|
|
|
||
|
|
public static class ClaimsExtensions
|
||
|
|
{
|
||
|
|
public static Guid GetUserId(this ClaimsPrincipal principal)
|
||
|
|
{
|
||
|
|
var value = principal.FindFirstValue(JwtRegisteredClaimNames.Sub)
|
||
|
|
?? principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
||
|
|
?? principal.FindFirstValue("sub");
|
||
|
|
if (value is not null && Guid.TryParse(value, out var id))
|
||
|
|
{
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
throw new UnauthorizedAccessException("Invalid user identity");
|
||
|
|
}
|
||
|
|
}
|