Search code examples
asp.net.netsql-serversessionweb-config

What session provider types exist for SQLServer?


If you wanted to implement an SQL session in your web.config, you'd usually have something simple like:

<sessionState mode="SQLServer" sqlConnectionString="myConnectionString"/>

But if you want to have a custom provider in order to do something like obscure the connection string with a configuration builder, you could write the following:

<sessionState mode="Custom" customProvider="SQLSessionProvider">
   <providers>
      <add name="SQLSessionProvider" connectionStringName="SQLSessionService" type=""/>
   </providers>
</sessionState>

<connectionStrings configBuilders="CS_Environment">
   <add name="SQLSessionService" connectionString="Environment_Key_Here" />
</connectionStrings>

The problem is that I don't know what type exists for mode=SQLServer. In my searching, I've seen examples for an OBDC session where type=ObdcSessionStateStore as well as various other session providers but none for SQLServer.

What session provider types exist for SQLServer?


Solution

  • The type you can use for this, and install via Nuget Package Manager, is

    Microsoft.AspNet.SessionState.SqlSessionStateProviderAsync

    which should also install SessionState.SessionStateModule. If you're installing this to your project for the first time, it'll create a <sessionState> template for you in your web.config. An example of how to use this is below:

    <connectionStrings configBuilders="CS_Environment">
       <add name="SQLSession_Connection" providerName="System.Data.SqlClient" connectionString="SQLSessionProvider-configBuilder_failed" />
    </connectionStrings>
    
    <sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom" customProvider="SqlSessionStateProviderAsync">
       <providers>
          <add name="SqlSessionStateProviderAsync" connectionStringName="SQLSession_Connection" type="Microsoft.AspNet.SessionState.SqlSessionStateProviderAsync, Microsoft.AspNet.SessionState.SqlSessionStateProviderAsync, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
       </providers>
    </sessionState>