[英]Asp.net Identity MVC, How to get logged in users' name in _Layout. User.Identity.Name gives me email
So my users should still login using their email adress, but I want my top menu to greet them by their Name. 因此,我的用户仍应使用其电子邮件地址登录,但是我希望我的顶级菜单通过其名称来问候他们。 I have the name implemented and everything, I just need a way of getting it to show. 我已经实现了名称,并且一切都已实现,我只需要一种显示它的方法。
Currently I have this in my top menu. 目前,我在顶部菜单中有此菜单。
Logged in as @User.Identity.Name
This gives me AppUser.EmailAddress, but i need the AppUser.Name 这给了我AppUser.EmailAddress,但是我需要AppUser.Name
This is my Login action, currently it only contains data from the loginviewmodel (email, password, rememberme). 这是我的登录操作,当前仅包含loginviewmodel中的数据(电子邮件,密码,记住我)。
public async Task<IActionResult> Login(LoginViewModel login, string returnUrl = null)
{
if (!ModelState.IsValid)
{
return View(login);
}
var result = await _signInManager.PasswordSignInAsync(
login.EmailAddress,
login.Password,
login.RememberMe, false);
if (!result.Succeeded)
{
ViewBag.Succeeded = false;
return View(login);
}
if (string.IsNullOrWhiteSpace(returnUrl))
{
return RedirectToAction("Index", "Home");
}
return Redirect(returnUrl);
The name exists in my AppUser class and my RegisterViewModel which is used by the register action. 该名称存在于我的AppUser类和RegisterAction使用的RegisterViewModel中。
I'm using Identity 3.0.0 but I welcome ways of solving it on other versions, 我正在使用Identity 3.0.0,但欢迎在其他版本上解决该问题的方法,
Try this: 尝试这个:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
Then you have user
which you could use it like: 然后,您拥有可以使用它的user
:
Var username=user.lastname;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.