[英]How to Mock GetUserId and IsAuthenticated from IIdentity without ControllerContext
[英]IIDentity does not contain a definition for 'GetUserId'
我正在嘗試使用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();
}
}
}
添加以下內容對我有用。 我還需要添加@Badri提到的NuGet Package Microsoft ASP.NET Identity Core 。
using Microsoft.AspNet.Identity; // NuGet: Microsoft ASP.NET Identity Core.
嘗試using System.Security.Principal;
添加using System.Security.Principal;
,因為擴展方法是該命名空間的一部分。 請參閱此處的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.