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?
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.
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.
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
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With