相关疑难解决方法(0)

ASP.NET MVC 5 - 身份.如何获取当前的ApplicationUser

我的项目中有一个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

c# identity asp.net-mvc-5 asp.net-identity

229
推荐指数
6
解决办法
29万
查看次数

如何在ASP.NET Core中获取当前登录的用户标识

在使用MVC5之前我已经完成了这个,User.Identity.GetUserId()但这似乎不适用于此.本User.Identity可是没有的GetUserId()方法

我在用 Microsoft.AspNet.Identity

asp.net asp.net-identity asp.net-core-mvc asp.net-core-1.0

139
推荐指数
12
解决办法
15万
查看次数

在ASP.NET Core 2中获取用户ID

我正在尝试在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)

现在这就是我需要的

asp.net-mvc asp.net-core-2.1

4
推荐指数
1
解决办法
5156
查看次数

在MVC中获取要在View中使用的UserId

我的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()的定义。

有人有解决方案吗?

提前致谢!

asp.net asp.net-mvc

3
推荐指数
1
解决办法
4515
查看次数