简体   繁体   中英

User.Identity.Name returns null in ASP.NET Core MVC application

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.

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

尝试这个

User.FindFirst(ClaimTypes.Name)?.Value 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM