【问题标题】:getUserId does not exist in User.Identity in rc1.finalrc1.final 中的 User.Identity 中不存在 getUserId
【发布时间】:2016-02-12 08:17:03
【问题描述】:

我正在使用 asp.net 5 和实体框架 7 来创建 Web 应用程序,但在使 User.Identity 工作时遇到了一些问题。在documentation 中,它表示它位于命名空间User.Identity.Core 中。当我通过 nuget 安装它时,它会找到方法。但是,我与UserManager 发生冲突,因为它说它同时存在于User.Identity.CoreUser.Identity.EntityFramework 中。如果我卸载User.Identity.EntityFramework,则不允许在我的用户模型中从IdentityUser 继承。我需要GetUserId 才能获取当前登录的客栈用户...我也尝试过使用

System.Web.HttpContext.Current.User.Identity.GetUserId(); 

但似乎有一个类似的问题,它说CurrentHttpContext 中不存在。

有人知道解决办法吗?

用 project.json 更新:

{
  "userSecretsId": "aspnet5-FrkKantine-1a8100b9-6fce-44b4-ac9d-6038ecf705f6",
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-rc1-final",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Razor": "4.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.CodeGenerators.Mvc": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnx50": {}
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  }
}

【问题讨论】:

  • 您所指的方法是关于 .NET Framework 4.x 和旧的 Identity。 System.Web.* 中的所有类型基本上都是指与 IIS 紧密耦合的旧 ASP.NET。例如,.NET Core 中的 HttpContext 抽象类在 Microsoft.AspNetCore.Http 中。一些关于 asp.net core 的非常基本的官方文档可以在这里找到docs.asp.net/en/latest。但它非常不完整,但对于一些基础知识来说是一个良好的开端
  • 你能发布你的 project.json,至少是依赖项/框架位吗?
  • 对于 ASP.NET Core 1.0 RTM/EntityFramework 1.0 Core 这个问题的解决方案是什么?

标签: c# asp.net entity-framework asp.net-identity asp.net-core


【解决方案1】:

由于 ASP.NET Core 1.0(以前的 ASP.NET 5)仍在开发中,因此情况经常发生变化。

请查看 GitHub Announcements tracker 了解 ASP.NET Core 1.0 版本的 API 更改(但不要在那里发布问题,这纯粹是 ASP.NET Core 团队发布公告)。

these announcements 之一是关于GetUsernameGetUserIdIsSignedIn 扩展方法的移动。

这些扩展方法引用的声明可通过以下方式配置 IdentityOptions 目前没有被 始终引用内置的扩展方法 ClaimTypes.NameIdentifier/名称。

这些方法已被移动以解决此问题:之前 => 之后

User.GetUserId => UserManager.GetUserId(User)
User.GetUserName => UserManager.GetUserName(User)
User.IsSignedIn => SignInManager.IsSignedIn(User)

编辑: 我刚刚注意到它适用于 RC2,因此可能还不适用于您,除非您使用夜间构建提要。

您的问题可能来自选择错误的包名,因为它们过去已重命名了六次

EntityFramework.MicrosoftSqlServerMicrosoft.AspNet.Identity.EntityFramework 应该适合你。 (使用 RC2,他们又重命名为 Microsoft.EntityFrameworkCore.*Microsoft.AspNetCore.*,所有版本都重置为 1.0)

【讨论】:

  • 谢谢!我实际上尝试查看公告,但我错过了那个。对不起(可能)愚蠢的问题,但是......如果 GetUserId 接受用户,我如何找到登录的用户?或者,如何让 User 对象传递给 GetUserId?
  • 登录用户仍在HttpContext 中。如果您在控制器内部(派生自 Controller 类型,否则与适用于 POCO 控制器的服务相同),您可以通过 HttpContext 访问它(或者只是 Context,它已被重命名几次)。如果您在服务中需要它,则必须将IHttpContextAccessor 注入您的服务并通过它的HttpContext 属性访问它
  • 非常感谢您的帮助!我实际上在 UserManager 中有方法,即使我认为我使用的是 RC1。我仍然无法让它为用户获取 ID,但我想我会停止尝试,而是使用名称,并要求用户使用唯一的用户名......
  • @JulieRøsok:将您的 projects.json 添加到问题中,可能是新旧包的混合。他们经常改变
  • Done :) 但它似乎也适用于 Name ......我本来打算使用电子邮件作为登录名/用户名,所以我认为它会很好。但是请随意查看json,看看包有没有问题!
【解决方案2】:

解决方案 1:

试试这个:

using Microsoft.AspNet.Identity;

然后:

string userId = User.Identity.GetUserId();

解决方案 2:

通常您应该确定用户是否已通过身份验证,然后获取用户信息/ID:

if (model != null)
    {
        model.IsUserAuthenticated = HttpContext.User.Identity.IsAuthenticated;

        if (model.IsUserAuthenticated)
        {
            model.UserName = HttpContext.User.Identity.Name;
        }
    }

【讨论】:

  • 您好,感谢您的回复。正如我试图在问题中解释的那样,如果我只是使用 Microsoft.AspNet.Identity,它找不到 GetUserId 方法。最新版本好像有一些变化,GetUserId不再是AspNet.Identity的一部分,而是在AspNet.Identity.Core中,这是一个不同的dll,如果我尝试安装它会导致冲突......您发布的其他代码用于获取名称,但我需要获取 Id 以进行查询(因为名称可能不是唯一的)
  • 在 asp.net core RC2 中,GetUserId() 默认不存在,也不是检查用户是否经过身份验证,难道你不能用 [Authorize] 装饰动作或控制器吗?
【解决方案3】:

如果您使用的是 .Net Core

需要参考:

using Microsoft.AspNet.Identity;
using System.Security.Claims;

用法: var id = User.GetUserId();(而不是User.Identity.GetUserId()

您提到的另一件事是关于 HttpContext.Current 为空,我假设您正在尝试从类库或 MVC 范围之外访问用户。

如果您需要从类库中访问 HttpContext,您不能像访问静态属性一样访问它。 需要在该类中注入

只需执行以下操作:

private readonly IHttpContextAccessor _httpContextAccessor;

public MyRepository(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}

然后你可以通过类中的所有方法来使用它,如下所示:

_httpContextAccessor.HttpContext.User.GetUserId();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多