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 :: c# streamwriter 
Csharp :: c# list index 
Csharp :: unity how get random color to material 
Csharp :: new color unity 
Csharp :: c# string array initialization 
Csharp :: c# countdown timer menutes 
Csharp :: c# switch 
Csharp :: color picker wpf 
Csharp :: hide datagrid column c# 
Csharp :: dotnet ef database update connection string 
Csharp :: write text files with C# 
Csharp :: how to find a gameobject in unity 
Csharp :: c# letters only 
Csharp :: know to which direction Vector.MoveTowards is moving unity 2D 
Csharp :: c# skip following code in loop 
Csharp :: enum get all values c# 
Csharp :: unity keycode for f 
Csharp :: reverse a string in c# 
Csharp :: c# get pressed key 
Csharp :: how get query from url in laravel 
Csharp :: c# indexof 
Csharp :: how to allow user import image c# 
Csharp :: unity object change sprite 
Csharp :: converting bitmap to byte array c# 
Csharp :: get description from enum c# 
Csharp :: c# move files from one directory to another 
Csharp :: c# insert character into string at position 
Csharp :: set parent of gameobject unity 
Csharp :: flip sprite in unity 
Csharp :: clear gridview data in c# 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =