![](/img/trans.png)
[英]Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication
[英]User.Identity.Name returns null in ASP.NET Core MVC application
請檢查下面的代碼。 這是我的 ASP.NET Core 2.1 MVC 應用程序的控制器。
問題是我已經在使用 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 是電子郵件,請執行上述操作。 這應該有效。 在這里找到更多信息
嘗試這個
User.FindFirst(ClaimTypes.Name)?.Value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.