Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying current username in _Layout view

I am wanting to display the current ApplicationUsers Firstname+Lastname on my navigation bar on my _Layout view. I've found that you can pass your viewbag from the current RenderedBody controller like so:

  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 a 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 like it's going against the grain and would be a huge pain to do for every view. What would be the standard in achieving this?

like image 1000
Matthew Spencer Avatar asked Jan 11 '17 01:01

Matthew Spencer


People also ask

How can I see logged in username in asp net?

You can display the name of the current user using the LoginName control. This control displays the user ID of the current logged-in user, whether the user's identity is established with ASP.NET login controls (and implicitly, ASP.NET membership) or with Integrated Windows authentication.

How does _layout Cshtml work?

So, the _layout. cshtml would be a layout view of all the views included in Views and its subfolders. The _ViewStart. cshtml can also be created in the sub-folders of the View folder to set the default layout page for all the views included in that particular subfolder.


2 Answers

You can inject the UserManager and SignInManager in to your view.

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

Then you can test if user login with SignInManager.IsSignedIn(User) and show user name with 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. Also you need to add these two using as well

@using Microsoft.AspNetCore.Identity
@using MyWebApp.Models
like image 104
Ahmar Avatar answered Sep 20 '22 19:09

Ahmar


For an ASP.Core Razor page, inject the UserManager into _Layout.cshtml

@using Microsoft.AspNetCore.Identity
@using webApp.Models
@inject UserManager<ApplicationUser> userManager
@{
    Layout = "/Pages/Shared/_Layout.cshtml";
    var user = await userManager.GetUserAsync(User);
    var displayName = user.DisplayName;
    var imagePath = user.GravatarImageUrl;
}

<h1>Manage your account</h1>

<div>
    <h4>Change your account settings</h4>
    <hr />
    <div class="row">
        <div class="col-md-3">
            <img src="@imagePath" alt="Avatar" />
            <h4>@displayName</h4>
            <partial name="_ManageNav" />
        </div>
        <div class="col-md-9">
            @RenderBody()
        </div>
    </div>
</div>

@section Scripts {
    @RenderSection("Scripts", required: false)
}
like image 37
djunod Avatar answered Sep 21 '22 19:09

djunod