mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
24 lines
778 B
C#
24 lines
778 B
C#
using System.Security.Claims;
|
|
using Kavita.Common;
|
|
using JwtRegisteredClaimNames = Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames;
|
|
|
|
namespace API.Extensions;
|
|
#nullable enable
|
|
|
|
public static class ClaimsPrincipalExtensions
|
|
{
|
|
public static string GetUsername(this ClaimsPrincipal user)
|
|
{
|
|
var userClaim = user.FindFirst(JwtRegisteredClaimNames.Name);
|
|
if (userClaim == null) throw new KavitaException("User is not authenticated");
|
|
return userClaim.Value;
|
|
}
|
|
|
|
public static int GetUserId(this ClaimsPrincipal user)
|
|
{
|
|
var userClaim = user.FindFirst(ClaimTypes.NameIdentifier);
|
|
if (userClaim == null) throw new KavitaException("User is not authenticated");
|
|
return int.Parse(userClaim.Value);
|
|
}
|
|
}
|