简体   繁体   中英

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

This is my Login action, currently it only contains data from the loginviewmodel (email, password, rememberme).

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.

I'm using Identity 3.0.0 but I welcome ways of solving it on other versions,

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:

Var username=user.lastname;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM