Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

asp.net core identity get user id

 public async Task<IActionResult> YourMethodName()
    {
        var userId =  User.FindFirstValue(ClaimTypes.NameIdentifier) // will give the user's userId
        var userName =  User.FindFirstValue(ClaimTypes.Name) // will give the user's userName

        ApplicationUser applicationUser = await _userManager.GetUserAsync(User);
        string userEmail = applicationUser?.Email; // will give the user's Email
    }
Comment

.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);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: cast char[] to string c# 
Csharp :: unity banner Ad position 
Csharp :: pyautogui not odwnloading 
Csharp :: how to compare datetime in c# 
Csharp :: how to add to a list only items that are not already in the list c# 
Csharp :: sorting a datatable in c# 
Csharp :: c# set cursor to loading and back 
Csharp :: mvc session key exists 
Csharp :: custom click event wpf button 
Csharp :: c# backup sql 
Csharp :: Get enum value from string or int 
Csharp :: c# debug writeline 
Csharp :: get the number of cpu c# 
Csharp :: datatable iqueryable c# linq 
Csharp :: pyqt minimize to tray icon 
Csharp :: take space separated input in c# 
Csharp :: c# override gethashcode 
Csharp :: c# chunk array linq 
Csharp :: deploy .net core 
Csharp :: what is list in c# 
Csharp :: unity 2d enemy patrol script 
Csharp :: c# loop string 
Csharp :: visitor pattern 
Csharp :: C# loop through the registry searching for keys containing 
Csharp :: center mouse unity 
Csharp :: C# Find first thing on a list 
Csharp :: c# $ string 
Csharp :: tailwind right 
Csharp :: unique field in class model .net core 
Csharp :: unity draw waypoins path 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =