我正在尝试在asp.net中创建一个后台服务,它检查会话超时并将用户重定向到超时页面(而不是登录页面)。我的代码就像这样
public class SessionTimeoutModule : IHttpModule
{
private static Timer timer;
//Test value
private const int interval = 1000 * 1 * 10;
public void Init( HttpApplication application )
{
if ( timer == null )
{
timer = new Timer( new TimerCallback( CheckSessionTimeout ),
application.Context, 0, interval );
}
}
private void CheckSessionTimeout( object sender )
{
HttpContext ctx = (HttpContext)sender;
if ( ctx.Session != null && ctx.Session.IsNewSession )
{
var cookie = ctx.Request.Headers["Cookie"];
if ( cookie != null )
{
if ( cookie.ToUpper().IndexOf( "ASP.NET_SESSIONID" ) >= 0 )
{
ctx.Response.Redirect( "" );
}
}
}
}
public void Dispose()
{
timer = null;
}
}
这里的问题是我无法在CheckSessionTimeout方法中获取会话值。它始终为空。如何在这里获得会议。
我查看了this解决方案,但它没有帮助。
答案 0 :(得分:1)
我担心你在这里走错了路。您不能使用像这样的HttpModule实现后台服务。你传递的HttpContext绑定了一个HTTP请求,我很确定你不应该像你想要的那样保留它。即使您可以检测到会话超时,也没有办法在没有活动请求的情况下将用户重定向到新页面。
您可能会发现this thread有帮助。
答案 1 :(得分:0)
我认为这里存在逻辑错误,即使您修复了代码,也永远不会得到一个结束的会话。
如果您可以访问会话,它将具有sessionID。你不能通过这种方式获得以超时结束的会话。
也许你应该使用global.asax的session_end。但我不确定它会有所帮助。
同样in here你可以看到,SessionID在Session.Abandon之前不会被替换。