简体   繁体   English

Asp.net Identity MVC,如何在_Layout中登录用户名。 User.Identity.Name给我发送电子邮件

[英]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.

相关问题 ASP.NET控制器基类User.Identity.Name - ASP.NET Controller Base Class User.Identity.Name User.Identity.Name 在 ASP.NET Core MVC 应用程序中返回 null - User.Identity.Name returns null in ASP.NET Core MVC application 如何在ASP.NET中使用User.Identity.Name作为SqlDataSource的参数? - How to use User.Identity.Name as a parameter for SqlDataSource in ASP.NET? 如何在MVC中使用User.Identity.Name显示名称而不是UserName - How to display Name instead of UserName with User.Identity.Name in MVC 会话与User.Identity.Name进行比较,以使用Windows身份验证检查ASP.net核心应用程序的登录用户名 - Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication 如何在 ASP.NET Core Identity 中控制器的构造函数中获取登录用户名 - How to get logged-in user name in the constructor of a controller in ASP.NET Core Identity 即使将身份验证模式设置为“Windows”,ASP.Net MVC4 User.Identity.Name也会变为空 - ASP.Net MVC4 User.Identity.Name getting empty even though set authentication mode as 'Windows' 在MVC4中模拟User.Identity.Name - mocking User.Identity.Name in MVC4 User.Identity.Name或GetUserName在MVC 5上不起作用 - User.Identity.Name or GetUserName not working on MVC 5 测试使用User.Identity.Name的Asp.Net控制器操作方法? - Test an Asp.Net Controller action method which uses User.Identity.Name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM