会话放弃在ASP.NET中不起作用

时间:2013-02-25 13:48:40

标签: c# asp.net iis-7 global-asax

我开发了一个网站,我在会话中存储数据。如果用户超出会话,我必须在会话中设置数据。

Logout.aspx:

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            WebUser user = (WebUser)Session["User"];
            Session["User"] = null;
            Session.Abandon();
            if (user != null)
                SentiWordnetUtils.LogYaz(user.uAdi + "\t Çıkış Yaptı");


            if ((Request.Cookies["OturumRef"] != null))
                Response.Cookies["OturumRef"].Value = string.Empty;

            Response.Redirect("Login.aspx");
        }
        catch (Exception ex)
        {
             LogYaz( "Oturum Sonlandırma Hatası "+ex.Message.ToString());
        }

    }

global.asax中的session_end函数:

  void Session_End(object sender, EventArgs e) 
    {
        List<TBL_SentiWordNet> tempList = (List<TBL_SentiWordNet>)Session["listProcess"];
        if (tempList == null)
            return;
        using (DataClassesDataContext dc = new DataClassesDataContext())
        {
            foreach (TBL_SentiWordNet word in tempList)
            {
                var a = (from i in dc.TBL_SentiWordNets where i.id == word.id select i).FirstOrDefault();

                a.state = 0;

            }
           dc.SubmitChanges();
        }
       Session["listProcess"]= null;
       Session["User"] = null;


    }

此代码适用于本地但不适用于IIS。 a.state绝不是0

的web.config:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <sessionState mode="InProc" timeout="30" />
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
  </system.web>
  <connectionStrings>
    <add name="myconnectionstring" connectionString="Data Source=127.0.0.1;Initial Catalog=mydb;Persist Security Info=True;User ID=userID;Password=password" providerName="System.Data.SqlClient" />
  </connectionStrings>
    <system.webServer>
        <defaultDocument>
            <files>
                <clear />
                <add value="default.aspx" />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="iisstart.htm" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

1 个答案:

答案 0 :(得分:1)

会话结束永远不会真正触发,因为当它执行时,它是在页面请求完成并且请求已经发送到客户端之后。

您需要在页面或基页类中执行session_end代码(对于这类事情非常方便)。

见以下S.O.链接:

Session_End does not fire?

What is the difference between Session.Abandon() and Session.Clear()