加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – FormsAuthentication.SignOut()不起作用

发布时间:2020-12-15 04:22:48 所属栏目:百科 来源:网络整理
导读:我正在开发一个带有安全部分的网站,即名为“PIP”的文件夹. 登录部分工作正常,但是当我单击注销时,用户仍然是已知的,并且如果他/她触摸安全部分,则不会被重定向到登录页面. 这是我的web.config: system.webauthentication mode="Forms" forms loginUrl="Log
我正在开发一个带有安全部分的网站,即名为“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>

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

FormsAuthentication.RedirectFromLoginPage(uid,false);

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

FormsAuthentication.SignOut();
Response.Redirect("~/Default.aspx",true);

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

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

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

提前致谢.

解决方法

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

您的网页上是否有任何到期日期?如果没有,页面可能仍在您的浏览器缓存中,并且不会调用服务器上的表单身份验证检查.

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

在你的Global.asax.cs中添加以下事件处理程序 – 如果你还没有 – 并在其上放置一个断点.如果你在调用LogOff后点击后续请求的断点,那么你可以破解cookie并查看它内部 – 我的猜测是你不会点击这个断点,因为请求是从缓存中提供的.

protected void Application_BeginRequest(object sender,EventArgs e) 
    {}

要破解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)
                {

                }
            }

在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";
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读