如何执行服务器端code中的asp.net会话变量期满之前? [英] How to execute server side code just before the asp.net session variable expires?

查看:198
本文介绍了如何执行服务器端code中的asp.net会话变量期满之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net网站,我创建在用户登录会话,我想眼前这个会议之前,执行expire.I会我有确定的问题,我应该写code数据库中的某些操作和我怎么会知道该会话即将过期。

In my asp.net website I am creating a session upon user login and I would like to perform some operations in the database just before this session will expire.I am having problem in determining where should I write code and how will I know the session is going to expire.

我不知道如果'的Global.asax适合我,因为我要检查手动创建会话(而不是浏览器实例)。

I am not sure if 'session_end' event of 'Global.asax' suits my requirements as the session I want to check is created manually(not a browser instance).

可能有人请把我在正确的方向?

Could someone please put me in the right direction?

感谢。

推荐答案

这可能是pretty棘手的,即因为当会话模式设置为InProc方式的Session_End中方法仅支持。你可以做的,是使用监视存储在会话的项目IHttpModule的,当会话到期触发一个事件。那里$ C $的CProject是一个例子在(HTTP://www.$c$cproject.com/KB/aspnet/SessionEndStatePersister.aspx),但其并非没有限制,例如它不webfarm方案工作。

This could be pretty tricky, namely because the Session_End method is only supported when the Session mode is set to InProc. What you could do, is use an IHttpModule that monitors an item stored in session, and fires an event when the Session expires. There is an example over on CodeProject (http://www.codeproject.com/KB/aspnet/SessionEndStatePersister.aspx), but its not without limitations, for instance it doesn't work in webfarm scenarios.

使用Munsifali的技术,你可以这样做:

Using Munsifali's technique, you could do:

<httpModules>
 <add name="SessionEndModule" type="SessionTestWebApp.Components.SessionEndModule, SessionTestWebApp"/>
</httpModules>

和然后在应用程序启动线了的模块

And then wire up the module at the application start:

protected void Application_Start(object sender, EventArgs e)
{
  // In our sample application, we want to use the value of Session["UserEmail"] when our session ends
  SessionEndModule.SessionObjectKey = "UserEmail";

  // Wire up the static 'SessionEnd' event handler
  SessionEndModule.SessionEnd += new SessionEndEventHandler(SessionTimoutModule_SessionEnd);
}

private static void SessionTimoutModule_SessionEnd(object sender, SessionEndedEventArgs e)
{
   Debug.WriteLine("SessionTimoutModule_SessionEnd : SessionId : " + e.SessionId);

   // This will be the value in the session for the key specified in Application_Start
   // In this demonstration, we've set this to 'UserEmail', so it will be the value of Session["UserEmail"]
   object sessionObject = e.SessionObject;

   string val = (sessionObject == null) ? "[null]" : sessionObject.ToString();
   Debug.WriteLine("Returned value: " + val);
}

随后,会议开始时,你可以在一些用户数据抛出:

Then, when the Session starts, you can throw in some user data:

protected void Session_Start(object sender, EventArgs e)
{
   Debug.WriteLine("Session started: " + Session.SessionID);

   Session["UserId"] = new Random().Next(1, 100);
   Session["UserEmail"] = new Random().Next(100, 1000).ToString() + "@domain.com";

   Debug.WriteLine("UserId: " + Session["UserId"].ToString() + ", UserEmail: " + 
                 Session["UserEmail"].ToString());
}

这篇关于如何执行服务器端code中的asp.net会话变量期满之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆