Display current username in _Layout view

I want to display the current first and last name of ApplicationUsers Firstname + Lastname in my navigation bar in my _Layout view. I found that you can transfer your bag from the current RenderedBody controller as follows:

  private readonly IHttpContextAccessor _httpContext;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly ApplicationUser _user;

    public HomeController(IHttpContextAccessor httpContextAccessor, UserManager<ApplicationUser> userManager) {
       _httpContext = httpContextAccessor;
       _userManager = userManager;
        _user = _userManager.Users.Single(u => u.Id == _httpContext.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
    }

And in the controller:

public IActionResult Index()
    {

        ViewBag.Username = $"{_user.FirstName} {_user.Surname}";
        return View();
    }

Finally, in my _Layout View:

<strong class="font-bold">@ViewBag.Username</strong>

This method seems to go against the grain and will be a huge pain for every point of view. What would be the standard in achieving this?

+8
source share
2 answers

You can enter UserManagerand SignInManagerin their appearance.

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

, SignInManager.IsSignedIn(User) UserManager.GetUserName(User)

@if (SignInManager.IsSignedIn(User))
{
  <form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
   <ul class="nav navbar-nav navbar-right">
    <li>
     <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
    </li>
    <li>
      <button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button>
    </li>
   </ul>
 </form>
}

PS. using,

@using Microsoft.AspNetCore.Identity
@using MyWebApp.Models
+4

(ApplicationUser):

// Inject SignInManager and UserManager in your _Layout.cshtml
@inject SignInManager<ApplicationUser> signInManager;
@inject UserManager<ApplicationUser> userManager;


 @if (signInManager.IsSignedIn(User))
                {
                   // Get the current logged in user
                    Task<ApplicationUser> GetCurrentUserAsync() => userManager.GetUserAsync(User);

                    var user = await GetCurrentUserAsync();

                    var firstName = user.FirstName;
                    var lastName = user.LastName;
                    var lastLogin = user.LastLogin;
                    var nationality = user.Nationality;
                    var phoneNumber = user.PhoneNumber;

                    <h4>@firstName @lastName</h4>                                                                     
                    <h4>@lastLogin</h4>                                                
                    <h4>@nationality</h4>                                                
                    <h4>@phoneNumber</h4>
                }
0

Source: https://habr.com/ru/post/1666444/


All Articles