IIDentity不包含“GetUserId”的定义

时间:2022-01-02 16:54:20

I'm trying to use the VS2015 MVC controller and specifically GetUserID on User.Identity

我正在尝试使用VS2015 MVC控制器,特别是User.Identity上的GetUserID

i have seen a number of similar questions relating to this which state to add reference to Microsoft.AspNet.Identity, however as you can see that is referenced.

我已经看到了许多与此相关的类似问题,其中声明要添加对Microsoft.AspNet.Identity的引用,但是正如您所看到的那样引用了它。

i assume i'm missing a package or something i just cannot figure it out

我假设我错过了一个包或其他东西,我无法弄明白

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

2 个解决方案

#1


10  

Try to add using System.Security.Principal;, since the extension method is part of that namespace. See the code here.

尝试使用System.Security.Principal;添加,因为扩展方法是该命名空间的一部分。请参阅此处的代码。

#2


8  

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

添加以下内容对我有用。我还需要添加@Badri提到的NuGet Package Microsoft ASP.NET Identity Core。

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

#1


10  

Try to add using System.Security.Principal;, since the extension method is part of that namespace. See the code here.

尝试使用System.Security.Principal;添加,因为扩展方法是该命名空间的一部分。请参阅此处的代码。

#2


8  

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

添加以下内容对我有用。我还需要添加@Badri提到的NuGet Package Microsoft ASP.NET Identity Core。

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