【问题标题】:Why User.Identity.IsAuthenticated is always true even after logout为什么即使在注销后 User.Identity.IsAuthenticated 也始终为真
【发布时间】: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


【解决方案1】:

我认为您可能正在使用 Windows 集成身份验证 - 尽管我不确定。

浏览器使用 Windows 集成身份验证 - 这意味着它会自动使用其 Windows 凭据登录用户 - 无需询问。

这就是 IsAuthenticated 始终为真的原因。

其他想法: 我没有尝试过下面的代码,但是有一个叫做 IISServerOptions 的东西。 你可以试试看是否适合你。

services.Configure<IISServerOptions>(options => 
{
    options.AutomaticAuthentication = false;
});

【讨论】:

  • 我使用了 IISOptions,因为 IISServerOptions 不起作用。使用后。我在 var user = _accessor.HttpContext.User.Identity.Name; 中得到空值,其中 _accessor 是 IHttpContextAccessor 的对象。所以在那之后我没有登录用户名。我正在从这个和数据库检查角色中收集登录用户,这样我的整个应用程序就是这样设计的。任何想法 ?提前致谢。
  • @LalitSingh 我也在苦苦挣扎
猜你喜欢
  • 2012-05-15
  • 2017-04-09
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-14
  • 2019-10-05
  • 1970-01-01
相关资源
最近更新 更多