【发布时间】:2019-01-03 22:09:46
【问题描述】:
我正在开发 AspnetCore 2.1 MVC 应用程序,它具有 Windows 集成身份验证。我已经使用 await HttpContext.SignInAsync() 实现了登录、注销功能。它是基于 cookie 的身份验证,cookie 在我提供的指定时间后过期。
我的问题是我必须在用户注销后从导航栏中隐藏注销。但在 _layout.cshtml 中,@User.Identity.Is 的身份验证始终为真,即使在用户点击注销后也是如此。有什么帮助吗?下面是我正在使用的代码
<div class="navbar-collapse collapse">
@if(User.Identity.IsAuthenticated)
{
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-area="" asp-controller="InitiatedCases" asp-action="Index">Initiated Cases</a></li>
<li><a asp-area="" asp-controller="SubmittedCases" asp-action="Index">Submitted Cases</a></li>
<li><a asp-area="" asp-controller="UserAccessLogs" asp-action="Index">User Access Log</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a asp-controller="Account" asp-action="Logout">Logout</a></li>
<li><p class="nav navbar-text navbar-right">@User.Identity.Name!</p></li>
</ul>
}
else
{
<p></p>
}
</div>
下面是登录功能
var usrRole = _dbContext.UserAccess.Where(r => r.UserId == loginUser.UserId).FirstOrDefault();
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, loginUser.UserId.ToString()));
identity.AddClaim(new Claim("DisplayName", loginUser.UserId));
if (usrRole != null)
{
identity.AddClaim(new Claim(ClaimTypes.Role, usrRole.UserRole.ToString()));
}
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity),
new AuthenticationProperties
{
IsPersistent = true,
IssuedUtc = DateTime.Now,
ExpiresUtc = DateTime.Now.AddMinutes(_iconfiguration.GetValue<double>("Session:TimeOutInMinutes")),
AllowRefresh = false
});
【问题讨论】:
-
向我们展示您是如何注销用户的。
-
你是如何登出用户的?当您注销用户时,您是否正在调用类似“await HttpContext.SignOutAsync(AuthenticationScheme)”的东西?
-
@Manny ,是的,我正在调用“等待 HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme)”
-
那应该是你的问题,因为你在问它。
-
另外,你解决过这个问题吗? ;)
标签: asp.net-mvc asp.net-core-2.1