24 lines
654 B
C#
24 lines
654 B
C#
using System.ComponentModel;
|
|
using System.Security.Claims;
|
|
|
|
namespace DD.Persistence.API;
|
|
|
|
public static class Extensions
|
|
{
|
|
public static T GetUserId<T>(this ClaimsPrincipal principal)
|
|
{
|
|
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;
|
|
|
|
}
|
|
}
|