2026-08-13 23:10:22 +07:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Microsoft.IdentityModel.JsonWebTokens;
|
|
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
namespace mws.backend.dotnet.api;
|
2026-08-13 23:10:22 +07:00
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|