ASP.NET MVC 5 - Identity. 如何获取当前的ApplicationUser。

257
我在我的项目中有一个文章实体,其中有名为"Author"的 "ApplicationUser" 属性。如何获取当前登录 "ApplicationUser" 的完整对象?在创建新文章时,我必须将 Article 中的 Author 属性设置为当前的 ApplicationUser
在旧的 Membership 机制中,这很简单,但在新的 Identity 方法中,我不知道该怎么做。我尝试以下方式:
1. 添加使用Identity扩展的语句: using Microsoft.AspNet.Identity; 2. 然后尝试获取当前用户: ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId()); 但我得到了以下异常:'System.String GetUserId(System.Security.Principal.IIdentity)' 方法无法转换为存储表达式。
来源=EntityFramework
11个回答

-1

我成功地通过以下代码获取了应用程序用户

var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var user = manager.FindById(User.Identity.GetUserId());
            ApplicationUser EmpUser = user;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接