title: How to store asp.net session state in SQL Server for Asp.Net MVC 4 application or WebRole application?
body: <p>How do I store session state in SQL Server for Asp.net mvc4 or Windows Azure Web Role application?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Why do I get this error?</p>
  
  <blockquote>
    <p><em>HTTP Error 500.23 - Internal Server Error</em><br>
    <em>An ASP.NET setting has been detected that does not apply in Integrated managed pipeline mode.</em></p>
  </blockquote>
  
  <p>My <code>web.config</code> looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"&gt;
        &lt;section name="webAssets" type="Telerik.Web.Mvc.Configuration.WebAssetConfigurationSection, Telerik.Web.Mvc" requirePermission="false" /&gt;
        &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
        &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
      &lt;/sectionGroup&gt;
    &lt;/configSections&gt;
    &lt;!--
      &lt;connectionStrings&gt;
          &lt;add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet2012620142020;Integrated Security=True" providerName="System.Data.SqlClient"/&gt;
      &lt;/connectionStrings&gt;
    --&gt;
    &lt;connectionStrings&gt;
      &lt;!--&lt;add name="HotDeployDbContext" connectionString="data source=JOE-WANG\WQ;initial catalog=HotDeploy;user id=sa;password=wq187731" providerName="System.Data.SqlClient"/&gt;--&gt;
      &lt;!--&lt;add name="HotDeployDbContext" connectionString="data source=tcp:z1782cwqws.database.windows.net,1433;initial catalog=HotDeployDB4CloudService;user id=hotdeployadmin@z1782cwqws;password=accela.123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" /&gt;--&gt;
      &lt;add name="HotDeployDbContext" connectionString="data source=tcp:xxxxxxxx.database.windows.net,1433;initial catalog=xxxxx;user idxxxxx@xxxx;password=dddd.123;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient"/&gt;
    &lt;/connectionStrings&gt;
    &lt;appSettings&gt;
      &lt;add key="webpages:Version" value="2.0.0.0" /&gt;
      &lt;add key="webpages:Enabled" value="true" /&gt;
      &lt;add key="PreserveLoginUrl" value="true" /&gt;
      &lt;add key="ClientValidationEnabled" value="true" /&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;customErrors mode="Off" /&gt;
      &lt;httpRuntime executionTimeout="14400" maxRequestLength="716800" /&gt;
      &lt;compilation targetFramework="4.0" /&gt;
      &lt;authentication mode="Forms"&gt;
        &lt;forms loginUrl="~/Account/Login" timeout="2880" /&gt;
      &lt;/authentication&gt;
      &lt;pages&gt;
        &lt;namespaces&gt;
          &lt;add namespace="System.Web.Helpers" /&gt;
          &lt;add namespace="System.Web.Mvc" /&gt;
          &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
          &lt;add namespace="System.Web.Mvc.Html" /&gt;
          &lt;add namespace="System.Web.Routing" /&gt;
          &lt;add namespace="System.Web.WebPages" /&gt;
          &lt;add namespace="Telerik.Web.Mvc.UI" /&gt;
        &lt;/namespaces&gt;
      &lt;/pages&gt;
      &lt;profile defaultProvider="DefaultProfileProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/profile&gt;
      &lt;membership defaultProvider="DefaultMembershipProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/membership&gt;
      &lt;roleManager defaultProvider="DefaultRoleProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/roleManager&gt;
      &lt;!--
      &lt;sessionState mode="InProc" timeout="30" customProvider="DefaultSessionProvider"&gt;
        &lt;providers&gt;
          &lt;add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" /&gt;
        &lt;/providers&gt;
      &lt;/sessionState&gt;
      &lt;httpHandlers&gt;
        &lt;add verb="GET,HEAD" path="asset.axd" validate="false" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" /&gt;
      &lt;/httpHandlers&gt;--&gt;
      &lt;!--before I added sessionState element . everything is fine.--&gt;
      &lt;sessionState mode="SQLServer" cookieless="true" 
      sqlConnectionString="data source=10.50.70.81; userid=sa; password=wq187731"
      timeout="300" 
      sqlCommandTimeout="10" /&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;security&gt;
        &lt;requestFiltering&gt;
          &lt;requestLimits maxAllowedContentLength="3000000000" /&gt;
        &lt;/requestFiltering&gt;
      &lt;/security&gt;
      &lt;validation validateIntegratedModeConfiguration="false" /&gt;
      &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
      &lt;directoryBrowse enabled="true" /&gt;
      &lt;handlers&gt;
        &lt;remove name="asset" /&gt;
        &lt;add name="asset" preCondition="integratedMode" verb="GET,HEAD" path="asset.axd" type="Telerik.Web.Mvc.WebAssetHttpHandler, Telerik.Web.Mvc" /&gt;
      &lt;/handlers&gt;
    &lt;/system.webServer&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="Microsoft.WindowsAzure.StorageClient" publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-1.7.0.0" newVersion="1.7.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>If I change the application pool to classical mode . I got IIS error page below ..</p>
  
  <blockquote>
    <p><em>HTTP Error 403.14 - Forbidden</em><br>
    <em>The Web server is configured to not list the contents of this directory.</em></p>
  </blockquote>