我的项目中有一个Article实体,其中包含ApplicationUser名为的属性Author.如何获取当前记录的完整对象ApplicationUser?在创建新文章时,我必须将Author属性设置Article为当前属性ApplicationUser.
在旧的成员机制中它很简单,但在新的身份识别方法中,我不知道如何做到这一点.
我试着这样做:
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
在使用MVC5之前我已经完成了这个,User.Identity.GetUserId()但这似乎不适用于此.本User.Identity可是没有的GetUserId()方法
我在用 Microsoft.AspNet.Identity
我正在尝试在ASP.NET Core 2.1 MVC项目中获取用户ID.
但是,我只能收到电子邮件.我几乎肯定必须有1/2行的方式来获取它(在ASP.NET MVC成员中它只是var loggedInUserId = User.Identity.GetUserId();
我到目前为止尝试过这样:
var loggedInUserId = User.Identity.ToString(); // Result = Name (E-mail)
// var loggedInUserId = User.Identity.Name; // Result (E-mail)
Run Code Online (Sandbox Code Playgroud)
现在这就是我需要的
我的MVC 5网络应用程序中存在以下问题。我使用数据库优先方法和实体框架创建了一个表[accountlist],该表对from [UserId]的[Id]列具有外键关系[AspNetUsers]。现在,相关accountlist.cshtml内容包含一个下拉菜单。我也尝试过,@Html.HiddenFor(model => model.UserId, new { @Value = HttpContext.Current.User.Identity.Name})但问题是我需要正确的用户名而不是用户名(我不希望[UserName]from [AspNetUsers]是主键)。
不幸的是,HttpContext.Current.User.Identity.GetUserId()如该解决方案中所建议的那样,Identity不包含GetUserId()的定义。
有人有解决方案吗?
提前致谢!