如何在ASP.NET Core RazorPage中的布局页面中接收cookie?
我在ASP.NET Core RazorPage中创建登录页面,以下代码是: 登录页面:
List<Claim> claims = new List<Claim>()
{
new Claim("Test","Test"),
new Claim(ClaimTypes.NameIdentifier as string,user.UserID.ToString() as string),
new Claim(ClaimTypes.Name,user.UserName),
new Claim(ClaimTypes.Role as string,user.UserRole.ToString() as string)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var claimPrincipal = new ClaimsPrincipal(identity);
HttpContext.SignInAsync(claimPrincipal);
return RedirectToPage("../Index");
我的_layout页面就是这样 _layout:
if (???????????)
{
<div>@User.Identity.Name</div>
<ul class="navbar-nav d-flex justify-content-end">
<li class="nav-item">
<a class="btn btn-primary" asp-page="/Auth/Logout">Logout</a>
</li>
</ul>
}
else
{
<ul class="navbar-nav d-flex justify-content-end flex-grow-1">
<li class="nav-item">
<a class="btn btn-primary" asp-page="/Auth/login">Login</a>
</li>
<li class="nav-item">
<a class="btn" asp-page="/Auth/Register">Register</a>
</li>
</ul>
}
问题是: 我应该放什么而不是问号?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确定用户是否已成功登录,
在_layout中的代码下面尝试:
在我的startup.cs
结果中:
Determine whether the user has successfully logged in,
Try below code in _layout:
In my startup.cs
result: