Search code examples
asp.net.netiis

.NET framework Session State


I restored a Database and I created a session state through this command

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql 
     -ssadd -sstype p -S IP_ADDRESS -U USER_ID sa1 -P USER_PASS

And in my web.config I have the following:

<sessionState mode="SQLServer" allowCustomSqlDatabase="true" 
        sqlConnectionString="Data Source=IP_ADDRESS;User Id=USER_ID;Password=USER_PASSWORD" cookieless="false" timeout="35"/>

I am using IIS to host the website. I noticed that there is Session State configuration there, and it is using InProcess Mode. Do I need to update the Session State in the IIS, or is my Web.config already enough?

I am getting erratic session expiry, and I am not sure what's causing the session expiry. Please help a newbie out, the docs I found on the internet is very confusing. TIA!

IIS Session State Settings


Solution

  • Yes, you have to change the above settings you have.

    You are to select SQL Server in your screen shot.

    enter image description here

    And in most cases, you should check the "allow custom database".

    When you change the above, then web config should result in something like this:

    <sessionState allowCustomSqlDatabase="true" 
          customProvider="DefaultSessionProvider" 
          mode="SQLServer" sqlConnectionString="ASPState">
    </sessionState>
    

    So, assuming you have setup the connection string in your web config, then the dropdown in IIS should have that connection string setting you have. If not, then you can use the create connection, and it will of course then modify web.config for you.

    So, in your web.config connection strings area, you need to have a connection string setup for the ASPState database.

    Hence:

    <add name="ASPState" 
    connectionString="Data Source=XX.XX.XXX.XXXX\SQLEXPRESS,database=ASPState;
    UID=MyUser;PWD=MyPass;APP=WebPortal;WSID=WebPortal"
    providerName="System.Data.SqlClient" />
    

    (above connection is on one line - above for ease of reading).

    Also, keep in mind, that if you launch SQL manager you will note that while a ASPState database is created?

    The session tables are created, used, and run as temp tables and are actually placed in system "tempdb" database!

    This can be rather confusing, since if you re boot SQL Server, then all temp db databases are deleted. However, in the ASPState database, you find some stored procedures that run on server startup that re-creates the session database. So, this is just a FYI, since it can be confusing that a ASPState database is created, but the actual session tables are created and used from the system "tempdb" database and NOT the ASPState database.

    The other issue of course is if you are using SQL Server Express or are you using the full edition of SQL Server?

    This matters, since the Express edition does not include the so-called SQL "agent" which is used to automatic run scheduled tasks by SQL Server. Those tasks include clearing out of the session database every 10 minutes, or some such.

    So, this means if you are using SQL Server Express, then the automated cleaning out and deleting of session rows (in that tempdb) does not occur. This suggests that you have to setup your own scheduled task (say using Windows Scheduler) to delete and clean out the session database (I suggest a daily routine to do this). So, keep in mind 2 things that are confusing:

    While a ASPState database is created when you run that script, the actual session tables used are created in tempdb.

    If you re-boot SQL Server, then tempdb is cleared out, and hence the session tables are also deleted.

    However, in ASPState, there are some stored procedures setup to run on SQL Server startup to re-create the session tables. This works fine for both SQL Server, and including the SQL express edition.

    However, since SQL Express does not have the SQL "agent" built in, then you need to setup your own scheduled task to clear out the session tables (else they will grow rather large without the session rows being automatic deleted).