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

吃可爱长大的小学妹 提交于 2019-12-24 01:38:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!