gpt4 book ai didi

c# - rc1.final 中的 User.Identity 中不存在 getUserId

转载 作者:行者123 更新时间:2023-11-30 20:20:48 25 4
gpt4 key购买 nike

我正在使用 asp.net 5 和 Entity Framework 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" ]
}
}

最佳答案

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

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

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

The claims that these extension methods refer to are configurable via IdentityOptions which was not currently being respected by the extensions methods which always referred to the build in ClaimTypes.NameIdentifier/Name.

These methods have been moved to address this: Before => After

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)

关于c# - rc1.final 中的 User.Identity 中不存在 getUserId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35357728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com