“ClaimsPrincipal”不包含“GetUserId”的定义

Kir*_*eed 3 asp.net-identity dnx

前段时间,我使用 ASP.Net 5 Web 应用程序模板创建了一个项目。我记得当时建造的项目。

现在,当我尝试构建我创建的旧项目时,出现错误

Severity    Code    Description Project File    Line    Suppression State
Error   CS1061  'ClaimsPrincipal' does not contain a definition for 'GetUserId' 
and no extension method 'GetUserId' accepting a first argument of type 'ClaimsPrincipal' could be found
 (are you missing a using directive or an assembly reference?)  KGWebsite.DNX 4.5.1, KGWebsite.DNX Core 5.0 
E:\EShared\Dev2016\KG.Website\KGWebsite\src\KGWebsite\Controllers\Web\ManageController.cs   291 Active
Run Code Online (Sandbox Code Playgroud)

当我使用相同的模板从头开始创建新项目时,新项目将构建。我在对两个项目之间的差异进行故障排除时遇到问题。

两者都使用 DNX4.5.1,它在解决方案资源管理器中显示为引用 Microsoft.AspNet.Identity (3.0.0-rc1-final)

当我在工作副本中钻取 Microsoft.AspNet.Http 的元数据时,我有

#region Assembly Microsoft.AspNet.Http.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Users\kirsten\.dnx\packages\Microsoft.AspNet.Http.Abstractions\1.0.0-rc1-final\lib\net451\Microsoft.AspNet.Http.Abstractions.dll
#endregion
Run Code Online (Sandbox Code Playgroud)

在非工作副本中,我有

#region Assembly Microsoft.AspNet.Mvc.ViewFeatures, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Users\kirsten\.dnx\packages\Microsoft.AspNet.Mvc.ViewFeatures\6.0.0-rc1-final\lib\net451\Microsoft.AspNet.Mvc.ViewFeatures.dll
#endregion
Run Code Online (Sandbox Code Playgroud)

我应该采取哪些步骤来纠正不起作用的项目?

Sam*_*ari 7

GetUserId()方法是在System.Security.Claims命名空间中实现的扩展方法。因此,要使用该方法,只需在类中添加命名空间:

using System.Security.Claims;
Run Code Online (Sandbox Code Playgroud)

  • 这在 RC2 中发生了变化 - 您现在需要注入您的 `UserManager` 实例并在其上调用 `GetUserID(User)。https://github.com/aspnet/Announcements/issues/140 (7认同)