.net core identity get user id
public static class ClaimsPrincipalExtensions
{
public static T GetLoggedInUserId<T>(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
if (typeof(T) == typeof(string))
{
return (T)Convert.ChangeType(loggedInUserId, typeof(T));
}
else if (typeof(T) == typeof(int) || typeof(T) == typeof(long))
{
return loggedInUserId != null ? (T)Convert.ChangeType(loggedInUserId, typeof(T)) : (T)Convert.ChangeType(0, typeof(T));
}
else
{
throw new Exception("Invalid type provided");
}
}
public static string GetLoggedInUserName(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Name);
}
public static string GetLoggedInUserEmail(this ClaimsPrincipal principal)
{
if (principal == null)
throw new ArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Email);
}
}
Also in C#:
- create expression func c# for use in where clause
- c# only letters
- slomotion in unity 3d
- c# join string array
- C# webclient submit form
- unity how to move an object to another object
- c# datetime dd/mm/yyy hh:mm:ss
- how to put double quotes in a string c#
- get user directory of file in c#
- how to deactivate objects through scripts in unity
- C# how to get public key for InternalsVisibleTo
- C# .net core convert int to enum
- c# read all text from a file
- isGrounded script for copy
- how to destroy a gameobject after some hits in unity 3d
- set decimal point c#
- how to reference scripts in other scenes unity
- s3 upload base64 .net core
- c# switct case
- how to parse a string to an integer c#
- run async methods within a sync process
- console application in c# ms crm
- c# null conditional operator if statement
- c# string code ascii