Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 mode:

<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?

like image 829
Exitos Avatar asked Aug 19 '11 13:08

Exitos


3 Answers

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.

like image 147
James Johnson Avatar answered Nov 20 '22 11:11

James Johnson


  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.

like image 41
trav.i.is Avatar answered Nov 20 '22 10:11

trav.i.is


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.

like image 1
Shiraz Bhaiji Avatar answered Nov 20 '22 12:11

Shiraz Bhaiji