162
  • 如何在MVC 5中获取当前登录用户的 ID ?我尝试了 StackOverflow 的建议,但它们似乎不适用于 MVC 5。
  • 此外,将内容分配给用户的 MVC 5 最佳实践是什么?(例如User应该有Items。我应该将用户存储IdItem吗?我可以使用导航属性扩展User类吗?List<Item>

我正在使用 MVC 模板中的“个人用户帐户”。

试过这些:

'Membership.GetUser()' 为空。

4

9 回答 9

319

如果您在 ASP.NET MVC 控制器中编码,请使用

using Microsoft.AspNet.Identity;

...

User.Identity.GetUserId();

值得一提的是User.Identity.IsAuthenticatedUser.Identity.Name无需添加上述using声明即可工作。但GetUserId()没有它就不会出现。

如果您在 Controller 以外的类中,请使用

HttpContext.Current.User.Identity.GetUserId();

在 MVC 5 的默认模板中,用户 ID 是存储为字符串的 GUID。

尚无最佳实践,但发现了一些有关扩展用户配置文件的有价值信息:

于 2013-08-26T19:15:44.257 回答
28

尝试类似:

var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;

适用于 RTM。

于 2013-11-06T13:09:23.307 回答
19

如果您希望 ApplicationUser 对象包含在一行代码中(如果您安装了最新的 ASP.NET 标识),请尝试:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

您将需要以下 using 语句:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
于 2016-06-27T06:17:01.603 回答
7

获取 ID 非常简单,您已经解决了这个问题。

不过,您的第二个问题涉及更多。

所以,这现在都是预发布的东西,但是您面临的常见问题是您正在使用新属性(或您的问题中的 Items 集合)扩展用户。

开箱即用,您将IdentityModel在 Models 文件夹下获得一个名为的文件(在撰写本文时)。在那里你有几节课;ApplicationUserApplicationDbContext。要添加您的集合,Items您需要修改ApplicationUser该类,就像您在实体框架中使用的普通类一样。事实上,如果您快速浏览一下引擎盖,您会发现所有与身份相关的类(用户、角色等)现在都只是带有适当数据注释的 POCO,因此它们与 EF6 配合得很好。

接下来,您需要对AccountController构造函数进行一些更改,以便它知道使用您的 DbContext。

public AccountController()
{
    IdentityManager = new AuthenticationIdentityManager(
    new IdentityStore(new ApplicationDbContext()));
}

现在为您的登录用户获取整个用户对象说实话有点深奥。

    var userWithItems = (ApplicationUser)await IdentityManager.Store.Users
    .FindAsync(User.Identity.GetUserId(), CancellationToken.None);

该行将完成工作,您将能够userWithItems.Items随心所欲地访问。

高温高压

于 2013-10-14T12:44:51.370 回答
0

我感觉到你的痛苦,我正在尝试做同样的事情。就我而言,我只想清除用户。

我创建了一个基本控制器类,我的所有控制器都从该类继承。在其中我覆盖OnAuthentication并设置filterContext.HttpContext.User to null

这是迄今为止我做到的最好的...

public abstract class ApplicationController : Controller   
{
    ...
    protected override void OnAuthentication(AuthenticationContext filterContext)
    {
        base.OnAuthentication(filterContext); 

        if ( ... )
        {
            // You may find that modifying the 
            // filterContext.HttpContext.User 
            // here works as desired. 
            // In my case I just set it to null
            filterContext.HttpContext.User = null;
        }
    }
    ...
}
于 2013-10-16T01:23:39.927 回答
0

在 .Net MVC5 核心 2.2 中,我使用 HttpContext.User.Identity.Name 。它对我有用。

于 2019-08-28T02:48:58.770 回答
0
        string userName="";
        string userId = "";
        int uid = 0;
        if (HttpContext.Current != null && HttpContext.Current.User != null
                  && HttpContext.Current.User.Identity.Name != null)
        {
            userName = HttpContext.Current.User.Identity.Name;              
        }
        using (DevEntities context = new DevEntities())
        {

              uid = context.Users.Where(x => x.UserName == userName).Select(x=>x.Id).FirstOrDefault();
            return uid;
        }

        return uid;
于 2019-02-08T09:34:54.813 回答
0

如果其他人有这种情况:我正在创建一个电子邮件验证来登录我的应用程序,所以我的用户还没有登录,但是我使用下面的内容来检查登录时输入的电子邮件,这是@firecape 解决方案的一种变体

 ApplicationUser user = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindByEmail(Email.Text);

您还需要以下内容:

using Microsoft.AspNet.Identity;

using Microsoft.AspNet.Identity.Owin;
于 2019-05-16T23:39:35.867 回答
0

这就是我获得 AspNetUser Id 并将其显示在主页上的方式

我将以下代码放在我的 HomeController Index() 方法中

ViewBag.userId = User.Identity.GetUserId();

在视图页面中调用

ViewBag.userId 

运行项目,您将能够看到您的 userId

于 2019-09-17T20:37:34.853 回答