请检查下面的代码。这是我的 ASP.NET Core 2.1 MVC 应用程序的 Controller 。
问题是我已经使用 cookie 验证用户,但 User.Identity.Name
返回 null。那么如何在 .NET Core 上获取当前的身份验证用户呢?
[HttpPost]
public IActionResult AddMainMenu(MainMenuItems mainMenuItems)
{
string userEmail = User.Identity.Name;
return Json("success");
}
提前致谢。
最佳答案
就用它吧-
var val = User.FindFirst(ClaimTypes.Email).Value;
如果您的声明 NameIdentifier 是电子邮件,则按上述操作。这应该有效。更多信息found here
关于c# - User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134312/