You can either get value in class or control.
The response of @Maycon F. Castro, is wrong, you can yes, get user name or identification 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();
}
Sources : Content, Claimsprincipal.
I hope I’ve helped.
You should take these values from within your controllers.
– Maycon F. Castro
worked well, I took the values in the controller and sent to the object I was going to use, vlw
– Leonardo Bressan
Friend I will put as an answer, if I can score as valid answer thank you.
– Maycon F. Castro