我正在尝试使用VS2015 MVC控制器,特别是User.Identity上的GetUserID
我已经看到了许多与此相关的类似问题,表示要添加对Microsoft.AspNet.Identity的引用,但是您可以看到它被引用。
我认为我错过了一个包或者我无法弄明白的东西
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();
}
}
}
答案 0 :(得分:11)
添加以下内容对我有用。我还需要添加Microsoft ASP.NET Identity Core
提到的NuGet包@Badri.using Microsoft.AspNet.Identity; // NuGet: Microsoft ASP.NET Identity Core.
答案 1 :(得分:9)
尝试添加using System.Security.Principal;
,因为扩展方法是该命名空间的一部分。请参阅代码here。