在ASP.NET MVC 5应用程序中,如何从给定的Username
获取UserId
并将其显示在View
?
注意 - 我不需要当前<{em> Username
的{{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;