User.Identity.Name在ASP.NET Core MVC应用程序中返回null [英] User.Identity.Name returns null in ASP.NET Core MVC application

查看:76
本文介绍了User.Identity.Name在ASP.NET Core MVC应用程序中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查下面的代码.这是我的ASP.NET Core 2.1 MVC应用程序的控制器.

Please check the code below. This is a controller of my ASP.NET Core 2.1 MVC application.

问题是我已经在使用经过cookie身份验证的用户,但是 User.Identity.Name 返回null.那我如何才能在.NET Core上获取当前的身份验证用户?

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");          
}

谢谢.

推荐答案

只需使用它-

var val = User.FindFirst(ClaimTypes.Email).Value;

如果您的声明NameIdentifier是Email,请执行上述操作.这应该工作.更多信息在此处找到

If your claim NameIdentifier is Email then do like above. This should work. More information found here

这篇关于User.Identity.Name在ASP.NET Core MVC应用程序中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆