public async Task<IActionResult>YourMethodName()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier) // will give the user's userIdvar 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
}
Posted by: Guest
on October-07-2020
4
.net core identity get user id
publicstaticclassClaimsPrincipalExtensions
{
publicstatic T GetLoggedInUserId<T>(this ClaimsPrincipal principal)
{
if (principal ==null)
thrownewArgumentNullException(nameof(principal));
var loggedInUserId = principal.FindFirstValue(ClaimTypes.NameIdentifier);
if (typeof(T) ==typeof(string))
{
return (T)Convert.ChangeType(loggedInUserId, typeof(T));
}
elseif (typeof(T) ==typeof(int) ||typeof(T) ==typeof(long))
{
return loggedInUserId !=null ? (T)Convert.ChangeType(loggedInUserId, typeof(T)) : (T)Convert.ChangeType(0, typeof(T));
}
else
{
thrownewException("Invalid type provided");
}
}
publicstaticstringGetLoggedInUserName(this ClaimsPrincipal principal)
{
if (principal ==null)
thrownewArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Name);
}
publicstaticstringGetLoggedInUserEmail(this ClaimsPrincipal principal)
{
if (principal ==null)
thrownewArgumentNullException(nameof(principal));
return principal.FindFirstValue(ClaimTypes.Email);
}
}
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email