IIDentity does not contain a definition for 'GetUserId'

I am trying to use the VS2015 MVC controller and in particular GetUserID for User.Identity

I saw a number of similar questions related to this that claim to add a link to Microsoft.AspNet.Identity, however, as you can see, it is referenced.

I assume that I am missing a package or something that I just cannot understand.

using Microsoft.AspNet.Identity; using Microsoft.AspNet.Mvc; using Web_Test.Models; namespace Web_Test.Controllers { public class TestController : Controller { public TestController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) { UserManager = userManager; SignInManager = signInManager; } public UserManager<ApplicationUser> UserManager { get; private set; } public SignInManager<ApplicationUser> SignInManager { get; private set; } public IActionResult Index() { //GetUserId() underlined in Red in VS2015 //IIDentity does not contain a definition for 'GetUserId' string currentUserId = User.Identity.GetUserId(); return View(); } } } 
+5
source share
2 answers

Try adding using System.Security.Principal; because the extension method is part of this namespace. See the code here .

+10
source

Adding the following worked for me. I also needed to add the Microsoft ASP.NET Identity Core NuGet package mentioned by @Badri.

 using Microsoft.AspNet.Identity; // NuGet: Microsoft ASP.NET Identity Core. 
+8
source

Source: https://habr.com/ru/post/1208700/


All Articles