You can either get value in the class or the control.
The response from @Maycon F. Castro , is wrong, you can get the username or ID using ClaimsPrincipal
, see the example below:
Class:
public static class MinhaClasse
{
public static string GetUserId(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}
return claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
}
public static string GetUserName(this ClaimsPrincipal claimsPrincipal)
{
if (claimsPrincipal == null)
{
throw new ArgumentNullException(nameof(claimsPrincipal));
}
return claimsPrincipal.FindFirst(ClaimTypes.Name)?.Value;
}
}
HomeController:
public IActionResult Index()
{
var id = Content(User.GetUserId()).Content; //"dd76f866-a04b-4517-ba1f-7bf35a1ae2c8"
var name = Content(User.GetUserName()).Content; //[email protected]
return View();
}
Fonts : Content , ClaimsPrincipal .
I hope I have helped.