English 中文(简体)
Session。放弃()不会立即放弃会话
原标题:Session.Abandon() does not abandon the session straight away

在我的ASP.NET web应用程序中,我在<code>Page_Load()</code>中调用<code>Session。我希望这会立即放弃会话,下次我引用<code>HttpContext.Current.session</code>时,应该创建一个新的会话。但是,在Global.asax中的Session_EndSession_Start处理程序上设置断点表示在页面完成渲染之前不会调用这些处理程序。

所以有两个问题:

1) 为什么?

2) 一旦调用了Session.Advanced(),我如何在页面生命周期内继续使用HttpContext.Current.Session

提前感谢!

最佳回答

http://msdn.microsoft.com/en-us/library/ms524310(v=vs.90).aspx

Look at the remarks section on the linked page. Looks like the session objects are only queued for deletion, and not deleted until the code finishes running.

问题回答

这是我的解决方案:

private void PurgeSession()
{
    try
    {
        Session.Clear();
    }
    catch (Exception) {  }

    try
    {
        Session.Abandon();
    }
    catch (Exception) {  }

    try
    {
        Session.RemoveAll();
    }
    catch (Exception) {  }

    try
    {
        Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId") 
                                {Expires = DateTime.Now.AddYears(-1)});
    }
    catch (Exception) {  }
}

这实际上是轨道轰炸的选择。

部分信息来源于:http://www.dotnetfunda.com/articles/article1395-how-to-avoid-the-session-fixation-vulnerability-in-aspnet-.aspx

Session.放弃()实际上一直等到页面呈现出来。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签