【问题标题】:IIDentity does not contain a definition for 'GetUserId'IIDentity 不包含“GetUserId”的定义
【发布时间】:2015-02-08 00:55:47
【问题描述】:

我正在尝试使用 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();
    }
}
}

【问题讨论】:

  • 身份验证模式是否设置为“表单”?

标签: c# asp.net-mvc visual-studio asp.net-core


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试添加using System.Security.Principal;,因为扩展方法是该命名空间的一部分。见代码here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多