我的项目中有一个Article实体,其中包含ApplicationUser
名为的属性Author
.如何获取当前记录的完整对象ApplicationUser
?在创建新文章时,我必须将Author
属性设置Article
为当前属性ApplicationUser
.
在旧的成员机制中它很简单,但在新的身份识别方法中,我不知道如何做到这一点.
我试着这样做:
为身份扩展添加using语句: using Microsoft.AspNet.Identity;
然后我尝试获取当前用户: ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
但我得到以下异常:
LINQ to Entities无法识别方法'System.String GetUserId(System.Security.Principal.IIdentity)'方法,并且此方法无法转换为商店表达式.来源=的EntityFramework
Gone Coding.. 436
这引入了为初学者提供额外上下文的新依赖关系,但是前进的用户数据库表发生了变化(过去2年中有3次),但API是一致的.例如,users
现在AspNetUsers
在Identity Framework中调用该表,并且几个主键字段的名称不断变化,因此几个答案中的代码将不再按原样运行.
另一个问题是对数据库的底层OWIN访问将使用单独的上下文,因此来自单独的SQL访问的更改可能会产生无效结果(例如,看不到对数据库所做的更改).同样,解决方案是使用提供的API,而不是尝试解决它.
在ASP.Net标识中访问当前用户对象的正确方法(截至此日期)为:
var user = UserManager.FindById(User.Identity.GetUserId());
或者,如果您有异步操作,请执行以下操作:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
FindById
要求您具有以下using语句,以便非异步UserManager
方法可用(它们是UserManager的扩展方法,因此如果您不包含此方法,您将只看到FindByIdAsync
):
using Microsoft.AspNet.Identity;
如果您根本不在控制器中(例如,您正在使用IOC注入),则完整地从以下位置检索用户ID:
System.Web.HttpContext.Current.User.Identity.GetUserId();
如果您不在标准帐户控制器中,则需要将以下内容(作为示例)添加到控制器:
////// Application DB context /// protected ApplicationDbContext ApplicationDbContext { get; set; } ////// User manager - attached to application DB context /// protected UserManagerUserManager { get; set; }
this.ApplicationDbContext = new ApplicationDbContext(); this.UserManager = new UserManager(new UserStore (this.ApplicationDbContext));
2015年3月更新
注意:Identity Framework的最新更新会更改用于身份验证的基础类之一.您现在可以从当前HttpContent的Owin Context访问它.
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
当通过远程数据库连接(例如,对Azure数据库的本地主机测试)使用EF和Identity Framework与Azure时,您可以随机点击可怕的"错误:19 - 物理连接不可用".由于原因被隐藏在Identity Framework中,您无法在其中添加重试(或似乎缺少重试.Include(x->someTable)
),您需要SqlAzureExecutionStrategy
在项目中实现自定义.
从ASP.NET Identity 3.0.0开始,这已被重构
//returns the userid claim value if present, otherwise returns null User.GetUserId();
ApplicationDbContext context = new ApplicationDbContext(); var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); ApplicationUser currentUser = UserManager.FindById(User.Identity.GetUserId()); string ID = currentUser.Id; string Email = currentUser.Email; string Username = currentUser.UserName;
它在答案的评论中,但没有人发布这个作为实际的解决方案.
你只需要在顶部添加一个using语句:
using Microsoft.AspNet.Identity;
我的错误,我不应该在LINQ查询中使用一个方法.
正确的代码:
using Microsoft.AspNet.Identity; string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
Ellbar的代码有效!您只需要添加使用.
1 - using Microsoft.AspNet.Identity;
并且...... Ellbar的代码:
2 - string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
使用此代码(in currentUser
),您可以处理已连接用户的常规数据,如果您需要额外数据...请参阅此链接
这引入了为初学者提供额外上下文的新依赖关系,但是前进的用户数据库表发生了变化(过去2年中有3次),但API是一致的.例如,users
现在AspNetUsers
在Identity Framework中调用该表,并且几个主键字段的名称不断变化,因此几个答案中的代码将不再按原样运行.
另一个问题是对数据库的底层OWIN访问将使用单独的上下文,因此来自单独的SQL访问的更改可能会产生无效结果(例如,看不到对数据库所做的更改).同样,解决方案是使用提供的API,而不是尝试解决它.
在ASP.Net标识中访问当前用户对象的正确方法(截至此日期)为:
var user = UserManager.FindById(User.Identity.GetUserId());
或者,如果您有异步操作,请执行以下操作:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
FindById
要求您具有以下using语句,以便非异步UserManager
方法可用(它们是UserManager的扩展方法,因此如果您不包含此方法,您将只看到FindByIdAsync
):
using Microsoft.AspNet.Identity;
如果您根本不在控制器中(例如,您正在使用IOC注入),则完整地从以下位置检索用户ID:
System.Web.HttpContext.Current.User.Identity.GetUserId();
如果您不在标准帐户控制器中,则需要将以下内容(作为示例)添加到控制器:
/// <summary> /// Application DB context /// </summary> protected ApplicationDbContext ApplicationDbContext { get; set; } /// <summary> /// User manager - attached to application DB context /// </summary> protected UserManager<ApplicationUser> UserManager { get; set; }
this.ApplicationDbContext = new ApplicationDbContext(); this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
2015年3月更新
注意:Identity Framework的最新更新会更改用于身份验证的基础类之一.您现在可以从当前HttpContent的Owin Context访问它.
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
当通过远程数据库连接(例如,对Azure数据库的本地主机测试)使用EF和Identity Framework与Azure时,您可以随机点击可怕的"错误:19 - 物理连接不可用".由于原因被隐藏在Identity Framework中,您无法在其中添加重试(或似乎缺少重试.Include(x->someTable)
),您需要SqlAzureExecutionStrategy
在项目中实现自定义.