通过UserId获取UserName并在View中显示

时间:2015-09-28 17:37:02

标签: asp.net-mvc-5 asp.net-identity

在ASP.NET MVC 5应用程序中,如何从给定的Username获取UserId并将其显示在View

注意 - 我不需要当前<{em> Username的{​​{1}}。

1 个答案:

答案 0 :(得分:1)

根据您的评论,您使用的是ASP.Net Identity。我假设你已经有了ApplicationUserManager。例如,

private ApplicationUserManager _userManager;
protected ApplicationUserManager UserManager
{
    get { return _userManager ?? (_userManager = 
     HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()); }
}

然后你可以调用 FindById 方法。

var user = UserManager.FindById(id);
string username = user.UserName;