persistence/Persistence.API/Extensions.cs

24 lines
651 B
C#
Raw Permalink Normal View History

using System.ComponentModel;
using System.Security.Claims;
namespace Persistence.API;
public static class Extensions
{
public static T GetUserId<T>(this ClaimsPrincipal principal)
{
2024-12-10 10:43:12 +05:00
ArgumentNullException.ThrowIfNull(principal, nameof(principal));
var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
ArgumentNullException.ThrowIfNullOrEmpty(loggedInUserId, nameof(loggedInUserId));
var result = TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(loggedInUserId);
ArgumentNullException.ThrowIfNull(result, nameof(result));
return (T)result;
}
}