问题
Please check the code below. This is a controller of my ASP.NET Core 2.1 MVC application.
Problem is that I am already using cookie authenticated user, but the User.Identity.Name
returns null. How can I get my current auth user on .NET Core then?
[HttpPost]
public IActionResult AddMainMenu(MainMenuItems mainMenuItems)
{
string userEmail = User.Identity.Name;
return Json("success");
}
Thanks in advance.
回答1:
Just use it-
var val = User.FindFirst(ClaimTypes.Email).Value;
If your claim NameIdentifier is Email then do like above. This should work. More information found here
来源:https://stackoverflow.com/questions/52134312/user-identity-name-returns-null-in-asp-net-core-mvc-application