How to store Sessionstate in Database for a hosted .NET MVC website

匆匆过客 提交于 2019-12-04 18:45:59
Dave Alperovich

The Problem is:

You are trying to use a defined Connection String with the SessionState Provider. This is very logical, but doesn't work because SessionState Provider still uses it's original definition from ASP.NET 1.0, while Connection Strings were introduced in ASP.NET 2.0.

This is an odd inconsistency that Microsoft never addressed.

In order to setup DBSession States:

1) Use your full connection string when you define your SessionState

<sessionState mode="SQLServer" 
    sqlConnectionString="data source=1.1.1.1,1433;user id=userid;password=**************" 
    sqlCommandTimeout="30" cookieless="true" timeout="1440" />

Very Important: Set cookieless="true" and in your login method make sure that rememberme=false. These steps are important to prevent the framework from using cookies for session state.

2) Create the schema on your sql server:

aspnet_regsql.exe -ssadd -d <Your Database> 
           -sstype c -S <Server> -U <Username> -P <Password>

3) Set Machine Key property in you web.config to bind your sessions and membership to your host:

Forms Authentication across virtual directories

http://aspnetresources.com/tools/machineKey


Keep in mind:

Using Database for session is performance costly and (as you've noticed) functionally tedious with all the parameter passing. This is far from a perfect solution.

Shared accounts often cause problems for sessions due to load balancing. In such cases, you are better off turning to Cookies. Your greater problem seems to be GoDaddy shared hosting policy of limiting Sessions timeouts (Cookies and sessions) for security reasons.

If performance becomes a problem, you may want to change hosting plans.


Machine Key: is meant to bind Membership to a Domain. When Machine Key matches, membership can be shared across multiple domains. This may make a difference even without DB session (and is worth a try).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!