asp.netsql-serverasp.net-mvcsession

Session timeout is not working while using SqlServer mode


I am developing ASP.Net MVC application.

We have used sessionState mode SQLServer and i have set timeout to 20 minutes.

<sessionState mode="SQLServer" 
              sqlConnectionString="data source=127.0.0.1;user id=sa;password=sa" 
              cookieless="false" 
              timeout="2" />

Code is something like this in web config.

I have also set login page.

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

Now when session expires i want to navigate user to login page.

I checked many things but i was unable to understand how it exactly works? and how can i navigate user login page on session expire?

It is working in InProc mode. I used it in same way and user is redirected to login on session expire.

But i am unable to accomplish same thing in SQLServer Mode.

I am unable to understand what i am missing?

I checked Session State and also found that Session timeout handled in SQLServer Mode

Edit :- I want to redirect user to login page whenever another http request is executed for that session.


Solution

  • Difference between InProc and SQLServer mode is that SQLServer relies on MSSQL job to remove the session. It actively doesn't prevent you from login again.

    See Session State Providers

    SqlSessionStateStore doesn't actively monitor the Expires field. Instead, it relies on an external agent to scavenge the database and delete expired sessions—sessions whose Expires field holds a date and time less than the current date and time. The ASPState database includes a SQL Server Agent job that periodically (by default, every 60 seconds) calls the stored procedure DeleteExpiredSessions to remove expired sessions.