FormsAuthentication.SignOut()不起作用

Fal*_*cko 6 c# asp.net authentication

我正在开发一个带有安全部分的网站,即名为"PIP"的文件夹.

登录部分工作正常,但是当我单击注销时,用户仍然是已知的,并且如果他/她触摸安全部分,则不会被重定向到登录页面.

这是我的web.config:

<system.web>

<authentication mode="Forms">
  <forms loginUrl="Login.aspx" name=".ASPXFORMSAUTH">
  </forms>
</authentication>

</system.web>

<location path="PIP">
<system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)

用户通过身份验证的登录页面:

FormsAuthentication.RedirectFromLoginPage(uid, false);
Run Code Online (Sandbox Code Playgroud)

在安全文件夹(PIP)的default.aspx页面上有一个注销按钮,该按钮后面的代码:

FormsAuthentication.SignOut();
Response.Redirect("~/Default.aspx", true);
Run Code Online (Sandbox Code Playgroud)

在页面"Default.aspx"是一个链接到〜/ PIP/Default.aspx,它应该被重定向到登录页面但不是.注意会议似乎不会受到退出的影响.

我尝试了很多选项,手动删除会话.Session.Clear,Session.Abandon但似乎没有任何效果.

我希望你们能指出我正确的方向!

提前致谢.

Cia*_*ran 2

您在注销之前、期间或之后是否打开了任何其他 IE 实例?如果没有,您可以发现该cookie仍然存在于IE的共享cookie元素中。

您的网页是否设置了过期时间?如果不是,该页面可能仍然在您的浏览器缓存中,并且不会调用服务器上的表单身份验证检查。

如果您关闭浏览器并尝试再次访问受保护的资源并且必须登录,那么它配置正确......会话cookie不用作表单身份验证过程的一部分,因此您不必担心它 - FormsAuthentication .SignOut() 是执行此操作的正确方法。

在您的 Global.asax.cs 中添加以下事件处理程序(如果您还没有)并在其上放置一个断点。如果您在调用 LogOff 之后为后续请求命中断点,那么您可以打开 cookie 并查看其内部 - 我的猜测是您不会命中此断点,因为请求是从缓存提供的。

    protected void Application_BeginRequest(object sender, EventArgs e) 
    {}
Run Code Online (Sandbox Code Playgroud)

要破解 cookie:

                HttpRequest currentRequest = HttpContext.Current.Request;

            // Attempt to get the Forms Auth Cookie from the Request
            HttpCookie authenticationCookie = currentRequest.Cookies[FormsAuthentication.FormsCookieName];

            if(authenticationCookie != null)
            {
                // Crack the Cookie open
                var formsAuthenticationTicket = FormsAuthentication.Decrypt(authenticationCookie.Value);

                // breakpoint here to see the contents of the ticket.
                if (formsAuthenticationTicket.Expired)
                {

                }
            }
Run Code Online (Sandbox Code Playgroud)

在 Firefox 或 Chrome 中尝试这一点也是值得的,因为它们似乎更擅长立即删除 cookie。

要禁用缓存,您可以将以下内容放在其中一个页面中:

    private static void SetImmediateExpiryOnResponse(HttpResponse response)
    {
        response.Cache.SetAllowResponseInBrowserHistory(false);
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        response.Cache.SetNoStore();
        response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        response.Expires = -1;
        response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        response.CacheControl = "no-cache";
    }
Run Code Online (Sandbox Code Playgroud)