如何得到当前的用户,如何使用用户类在 MVC5?

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

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

试过这些:

“ Membership. GetUser ()”为空。

224004 次浏览

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

using Microsoft.AspNet.Identity;


...


User.Identity.GetUserId();

值得一提的是,User.Identity.IsAuthenticatedUser.Identity.Name将在不添加上述 using语句的情况下工作。但是没有它 GetUserId()就不会存在。

如果您所在的类不是 Controller,请使用

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

在 MVC5的默认模板中,用户 ID 是一个以字符串形式存储的 GUID。

目前还没有最好的实践,但是找到了一些关于扩展用户配置文件的有价值的信息:

拿到身份证很简单,你已经解决了。

不过你的第二个问题有点复杂。

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

开箱即用,你会得到一个名为 IdentityModel的文件,位于 Model 文件夹下(在编写本文时)。这里有两个类: ApplicationUserApplicationDbContext。要添加您的 Items集合,您需要修改 ApplicationUser类,就像您在 Entity Framework 中使用的普通类一样。事实上,如果你快速看一下引擎盖下面,你会发现所有的身份相关的类(用户,角色等..。.)现在只是带有适当数据注释的 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,如你所愿。

高温

我能感受到你的痛苦,我也想这么做。在我的例子中,我只想清除用户。

我已经创建了一个基控制器类,所有控制器都是从该类继承的。我在里面覆盖了 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;
}
}
...
}

试试这样:

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

和 RTM 一起工作。

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

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

你需要以下的使用语句:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
        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;

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

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

你亦须符合以下条件:

using Microsoft.AspNet.Identity;

还有

using Microsoft.AspNet.Identity.Owin;

在.Net MVC5 core 2.2中,我使用 HttpContext. User. Identity. Name。

这就是我如何得到一个 AspNetUserId 并在我的主页上显示它

我在 HomeController Index ()方法中放置了以下代码

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

在视图页面中调用

ViewBag.userId

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