本文介绍了重定向到Session_End中的事件另一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动重定向到登录会话时超时页。

I would like to auto-redirect to login page when session time outs.

在web.config文件中,我有以下code

In web.config file, i have the following code

<configuration>
    <system.web>
       <sessionState mode="InProc"  timeout="1"/>
    </system.web>
</configuration>

在Global.asax文件 -

In Global.asax file-

protected void Session_End(object sender, EventArgs e)
{
    Response.Redirect("LoginPage.aspx");
}

但经过超时,我收到以下错误:

But after time-out, i am receiving the following error:

HttpException是由用户code未处理。

响应不可用在这方面。

任何线索来解决这个问题?

Any clue to solve this issue?

感谢。

推荐答案

Session_End中被称为会话结束时 - 最后一个请求(例如,如果浏览器处于非活动状态或关闭)后,通常20分钟结果
由于没有要求还有没有反应。

Session_End is called when the session ends - normally 20 minutes after the last request (for example if browser is inactive or closed).
Since there is no request there is also no response.

我会建议做 Application_AcquireRequestState 重定向如果没有活动会话。记住通过检查当前URL来避免环路。

I would recommend to do redirection in Application_AcquireRequestState if there is no active session. Remember to avoid loops by checking current url.

编辑:我没有内置的身份验证.Nets风扇,例进去的Global.asax

I'm no fan of .Nets built in authentication, Example goes in Global.asax:

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        try
        {
            string lcReqPath = Request.Path.ToLower();

            // Session is not stable in AcquireRequestState - Use Current.Session instead.
            System.Web.SessionState.HttpSessionState curSession = HttpContext.Current.Session;

            // If we do not have a OK Logon (remember Session["LogonOK"] = null; on logout, and set to true on logon.)
            //  and we are not already on loginpage, redirect.

            // note: on missing pages curSession is null, Test this without 'curSession == null || ' and catch exception.
            if (lcReqPath != "/loginpage.aspx" &&
                (curSession == null || curSession["LogonOK"] == null))
            {
                // Redirect nicely
                Context.Server.ClearError();
                Context.Response.AddHeader("Location", "/LoginPage.aspx");
                Context.Response.TrySkipIisCustomErrors = true;
                Context.Response.StatusCode = (int) System.Net.HttpStatusCode.Redirect;
                // End now end the current request so we dont leak.
                Context.Response.Output.Close();
                Context.Response.End();
                return;
            }
        }
        catch (Exception)
        {

            // todo: handle exceptions nicely!
        }
    }

这篇关于重定向到Session_End中的事件另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:08