【问题标题】:ASP.NET global.asax end_session (session never end)ASP.NET global.asax end_session(会话永不结束)
【发布时间】:2013-01-31 20:06:29
【问题描述】:

我遇到了结束会话的问题。当我单击“WebForms.asax”中的按钮时,会话计数时间从零到一分钟(如果我单击此按钮会话将永远不会结束)。我给了价值 1 分钟的时间来结束所有会话,当我不点击网站时它会起作用。你知道问题出在哪里吗?

我在这里开始会话:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Session_Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["UserAuthentication"] = "dddd";
            TypSession.InnerHtml = "<a href='WebForm1.aspx'>sdsds</a>";
        }
    }
}

另一个网站(WebForm1.aspx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Session_Test
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            example.InnerHtml = Session["UserAuthentication"].ToString()+"         "+ Session.Timeout.ToString();;
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
            Session.Abandon();

        }
    }
}

global.asax

 void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 1; 
        }

        void Session_End(object sender, EventArgs e)
        {
            Response.Redirect("http://localhost:51888/Default.aspx");
        }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:
    Response.Redirect(Request.RawUrl);
    

    将结束你的响应,它与调用相同:

    Response.Redirect(Request.RawUrl, true);
    

    bool 代表停止响应,因此您的 Session.Abandon() 将永远不会被调用。如果您想在重定向后执行 Session.Abandon(),则应使用 false 调用重定向:

    Response.Redirect(Request.RawUrl, false);
    

    Redirect MSDN

    【讨论】:

    • 每次访问网站页面时,会话超时将重置为 1 分钟。
    • 如何结束会话?例如离开网站的用户?还是刷新网站?
    • Session.Abandon() 将在执行/渲染页面时结束会话。用户离开时无需结束会话,会话由iis处理并结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多