【问题标题】:session abandon is not working in ASP.NET会话放弃在 ASP.NET 中不起作用
【发布时间】:2013-02-10 16:27:00
【问题描述】:

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

注销.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>

【问题讨论】:

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


    【解决方案1】:

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

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

    请参阅以下 S.O.链接:

    Session_End does not fire?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2015-02-10
      • 2016-07-11
      • 2021-11-07
      • 2012-02-12
      相关资源
      最近更新 更多