会话过期URL错误

Session Expire URL bug

本文关键字:错误 URL 过期 会话      更新时间:2024-06-18

我有一个会话过期方法,它使用cookie中的倒计时在20分钟后注销用户。当用户成功注销时,它可以正常工作。唯一的问题是之后的URL显示为

localhost:8091/登录?ReturnUrl=%2Login%2LogOut

当它在/Login之后不应该有任何内容时。我正在使用C#和ASP。NET 4与MVC。

这是用于重定向用户的JS。。。

        function startCountdown(timeLeft) {
            if (countDownInterval > 0) return;
            $('#idletimeoutcount').text(countDown);
            $('#idletimeout').slideDown();
            countDown = parseInt(timeLeft / 1000);
            countDownInterval = setInterval(function() {
                $('#idletimeoutcount').text(countDown);
                var secondsLeft = countDown--;
                document.title = "Warning! " + secondsLeft + " seconds left until logged out";
                if (countDown <= 0) {
                  //  console.log('finished');
                    window.location.href = '/Login/LogOut';
                }
            }, 1000);
        }

以及注销功能:

   public ActionResult LogOut()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Login");
    }

有什么方法可以使url为localhost:8091/Login吗?(由于堆栈溢出,HTTP被删除)

这可能与包含以下标签的web配置有关:

          <authentication mode="Forms">
  <forms name="SqlAuthCookie" loginUrl="Login" timeout="20" slidingExpiration="true" />
</authentication>   

您不能删除url的ReturnUrl参数。这是表单身份验证的工作方式,请查看本文。如何从url中删除returnurl?

在WebForms中,我使用此代码来执行签出机制:

FormsAuthentication.SignOut();
Session.Abandon();
FormsAuthentication.RedirectToLoginPage();

FormsAuthentication是为处理登录过期而构建的。为什么不让它?您已经设置了参数:

timeout="20"

让您的javascript在时间到期后重定向到登录页面。

我遇到的解决错误的最佳解决方案是通过以下操作剥离返回URL:

        if (!string.IsNullOrEmpty(Request.QueryString["returnUrl"]))
        {
            return RedirectToAction("Index", "Login");
        }

这会检查他们是否是添加的returnURL,如果是,它会将您引导到登录页面。这是苦乐参半的,因为它解决了我的错误,但现在用户无法返回到会话到期前的页面。