我想在_Layout视图的导航栏上显示当前的ApplicationUsers Firstname + Lastname。我发现您可以从当前的RenderedBody控制器传递您的视图包,如下所示:
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);
}
在控制器中:
public IActionResult Index()
{
ViewBag.Username = $"{_user.FirstName} {_user.Surname}";
return View();
}
最后在我的_Layout视图中:
<strong class="font-bold">@ViewBag.Username</strong>
这种方法似乎与谷物相悖,对每一种观点来说都是一种巨大的痛苦。实现这一目标的标准是什么?
答案 0 :(得分:3)
您可以将UserManager
和SignInManager
注入视图。
@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
答案 1 :(得分:0)
您可以从您的扩展用户(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>
}
答案 2 :(得分:0)
对于ASP.Core Razor页面,将UserManager注入_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)
}
答案 3 :(得分:0)
最新答案,
在您的_Layout.cshtml
中:
@using System.Security.Claims
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor
@{
string name =
httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
}