c# - User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null

标签 c# asp.net-core-mvc

请检查下面的代码。这是我的 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/

相关文章:

c# - 尝试执行 SQL 查询时出现无效对象错误

c# - 如何对数据库中的用户使用 Windows 身份验证

c# - 在 ASP.NET 5 中获取访问 token

visual-studio-2015 - 缺少脚手架 ASP.Net Core

c# - 为什么 .NET 事件有一个对象类型而不是 T 类型的发送者参数?

c# - 打开 XML Excel。创建文件后无法打开打印对话框。文件有卡住行

c# - 获取单元格占用的行数 Itextsharp

asp.net core mvc 中的 CKEditor 数据绑定(bind)

c# - 结果过滤器在 ASP.NET Core 中如何工作?

c# - Xaml TextBlock 设置圆角