【问题标题】:MVC - Layout keeps displaying menus after logging outMVC - 退出后布局继续显示菜单
【发布时间】:2016-09-26 19:40:31
【问题描述】:

我目前正在开发一个遵循复数视觉指南的网站。 Web 应用程序有两个主要问题:

  1. 注销后,以下if 语句下的项目仍然显示:

                        @if (User.Identity.IsAuthenticated)
                    {
                        <li class="nav-item btn-group">
                            <a class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fa fa-user"></i> <span class="hidden-sm-down"> @User.Identity.Name </span> </a>
                            <div class="dropdown-menu">
                                <a class="dropdown-item" asp-controller="Account" asp-action="Account"> Account <i class="fa fa-user" aria-hidden="true"></i> </a>
                                <a class="dropdown-item" asp-controller="Settings" asp-action="Settings"> Settings <i class="fa fa-gear" aria-hidden="true"></i> </a>
                                <div class="dropdown-divider"></div>
                                <a class="dropdown-item" asp-controller="Logout" asp-action="Logout"> Logout <i class="fa fa-sign-out" aria-hidden="true"></i> </a>
                            </div>
                        </li>
                    }
    
  2. 第二个问题是对登录的用户隐藏菜单:

                        @if (!User.Identity.IsAuthenticated)
                    {
                        <li class="nav-item @(ViewContext.RouteData.Values["Controller"].ToString() == "Contact" ? "active" : "")">
                            <a class="nav-link" asp-controller="Contact" asp-action="Contact">Contact</a>
                        </li>
                        <li class="nav-item @(ViewContext.RouteData.Values["Controller"].ToString() == "About" ? "active" : "")">
                            <a class="nav-link" asp-controller="About" asp-action="About">About</a>
                        </li>
                    }
    

如果 User.Identity.IsAuthenticated 返回一个布尔值,我不确定为什么这不起作用。

Picture of my NavBar displaying menus after login out

如果需要,我会提供更多详细信息。提前谢谢!

【问题讨论】:

  • 您能否尝试在您的if 语句上设置断点并检查以确保IsAuthenticated 的值正确?您是否尝试过其他任何方法来找出造成这种情况的原因?
  • 另外,我复制粘贴了你在这里的内容,我正在使用 Windows 身份验证,我只是看到了第一个 if 的内容,正如我所期望的那样,因为我已登录。User.Identity.IsAuthenticated 对我来说是 true。 (我也在使用 fontawesome 所以我看到了图标,但这并不重要)
  • 那么当单击注销时,剃刀布局视图会在异步公共异步任务 Logout() 完成之前呈现?
  • 我不确定,我根本没有尝试注销,我只是粘贴了你的代码并点击运行,因为我是通过 Windows 身份验证自动登录的,所以我只看到在检查User.Identity.IsAuthenticatedif 中的第一部分。
  • 对于我的第一个问题,如果我刷新页面,用户名下拉菜单会按预期消失。但我希望它在他们被重定向到注销页面后立即消失。

标签: c# .net asp.net-mvc razor asp.net-identity


【解决方案1】:

当您使用 SignOut() 时,用户将在下一个 (http) 请求时注销。

所以

  1. 做一个重定向
  2. 或强制注销。似乎只有在非核心 ASP.Net 中才有可能:要立即注销,请使用默认值清除 User 属性:

    FormsAuthentication.SignOut();
    
     //log off immediately
     HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
    

【讨论】:

  • 我正在使用 Microsoft.AspNetCore.Identity 的 SignInManager。我不确定如何立即强制注销。
  • 啊 .Net Core 不清楚。 Afaik 这在 .Net 核心中是不可能的
  • 我只需要为注销页面创建一个单独的布局。 ://
猜你喜欢
  • 1970-01-01
  • 2014-05-06
  • 2019-01-23
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多