开发者

Why isnt the session timeout working when set to SqlServer?

I have the line:

<sessionState mode="SQLServer" sqlConnectionString="Data Source=localhost;User Id=sa;Password=test;" timeout="1" />

Which stores the session in a sql state server. However it does not timeout properly after one minute.

When I change the line to use InProc mod开发者_开发技巧e:

<sessionState mode="InProc" sqlConnectionString="Data Source=localhost;User Id=sa;Password=test;" timeout="1" />

It does timeout after one minute.

Any ideas why this is happening? How can I get it to timeout when using SqlServer?


If you're using SQL Server Express, it's because there is no SQL Server Agent to execute the DeleteExpiredSessions procedure.

Here is a possible workaround to the problem:

In the stored procedure that updates the session, I have added SQL from the DeleteExpiredSessions procedure to the update session stored procedure (I can't remember the name) so it checks for old sessions, deletes the old session, and then updates the current sessions. The stored procedure that updates the session runs on every click anyway, so I added two more lines that remove old sessions before the session is updated. This seems to work fine.


  1. Make sure the SQL Server Agent service is running

  2. Right-click on the SQL Server Agent job named ASPState_Job_DeleteExpiredSessions and click "View History" - Make sure this job is running successfully every 1 minute.

In my case, somehow (probably during one of multiple installs involving named instances) my SQL Server Agent had its property Connection -> Alias Localhost Server set to just the server name and not the servername\instancename.

When this change happened, the ASPState_Job_DeleteExpiredSessions appeared to run indefinitely and could not be stopped. So, I attempted to re-start the SQL Server Agent service, but it would not start back up. However, once I changed the Connection -> Alias Localhost Server property to servername\instancename, SQL Server Agent started right back up and the ASPState_Job_DeleteExpiredSessions job started running successfully once a minute like it should.....and this obviously solved my timeout problem.


You may be able to control the timeout of the session by setting the timeout of the forms authentication cookie:

FormsAuthenticationTicket authTicket = 
new FormsAuthenticationTicket(1, // version
txtUserName.Text,
DateTime.Now, 
DateTime.Now.AddMinutes(1),
false,@"\");

That way the user looses contact with the session after 1 min.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜