我正在使用asp.net 5和实体框架7来创建一个web应用程序,我在让User.Identity工作时遇到了一些问题。在文档中,它表示它位于名称空间User.Identity.Core中。当我通过nuget安装它时,它会找到这个方法。但是,我与UserManager有冲突,因为它说它同时存在于User.Identity.Core和User.Identity.EntityFramework中。如果卸载User.Identity.EntityFramework,则不允许在用户模型中继承IdentityUser。我需要GetUserId才能让现在登录的客栈用户.我也试过用
System.Web.HttpContext.Current.User.Identity.GetUserId(); 但是似乎也有一个类似的问题,它说Current在HttpContext中不存在。
有人知道解决这个问题的办法吗?
用更新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" ]
}
}发布于 2016-02-12 10:01:55
由于ASP.NET Core1.0(以前的ASP.NET 5)仍在开发中,所以情况经常发生变化。
请查看GitHub 公告跟踪器 for API更改ASP.NET Core1.0版本(但是不要在那里发布问题,这完全是由ASP.NET Core发布公告)。
这些公告之一是关于GetUsername、GetUserId和IsSignedIn扩展方法的移动。
这些扩展方法所引用的声明是通过IdentityOptions进行配置的,这些扩展方法目前不受扩展方法的尊重,这些扩展方法总是引用ClaimTypes.NameIdentifier/Name中的构建。 这些方法已被移动以解决以下问题:在=>之后 User.GetUserId => UserManager.GetUserId(用户) User.GetUserName => UserManager.GetUserName(用户) User.IsSignedIn => SignInManager.IsSignedIn(用户)
编辑:我刚刚注意到它是针对RC2的,所以很可能还不适用于您,除非您使用夜间构建提要。
您的问题可能来自选择了错误的包名,因为它们在过去已经被重命名了六次。
EntityFramework.MicrosoftSqlServer和Microsoft.AspNet.Identity.EntityFramework应该是适合你的。(使用RC2,他们又重命名为Microsoft.EntityFrameworkCore.*和Microsoft.AspNetCore.*,所有版本都重置为1.0)
发布于 2016-02-12 08:34:57
解决方案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;
}
}发布于 2016-02-12 14:23:43
如果您使用的是.Net Core <= RC1更新1,则仍然可以使用GetUserId()扩展。
所需参考资料:
using Microsoft.AspNet.Identity;
using System.Security.Claims;用法:var id = User.GetUserId(); (而不是User.Identity.GetUserId())
您提到的另一个不同的事情是HttpContext.Current为null,我假设您试图从类库或MVC的作用域之外访问用户。
如果您需要从类库中访问HttpContext,您不能仅仅像一个静态属性一样访问它。--它需要注入到类中。
只需做以下几点:
private readonly IHttpContextAccessor _httpContextAccessor;
public MyRepository(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}然后,您可以通过类中的所有方法使用它,如下所示:
_httpContextAccessor.HttpContext.User.GetUserId();https://stackoverflow.com/questions/35357728
复制相似问题