Search code examples
sql-serverasp.net-mvc-3sessionsession-storage

Loosing authentication when using SQLServer for storing authentication


I was using InProc mode for storing sessions in my mvc3 aplication, but because of the restarting of IIS about every 20 minutes, a changed storing authentication to SQLServer. Connection to database is fine, the session are storing, but authentication doesnt last very long its lost after the restart

Webconfig

<sessionState mode="SQLServer" timeout="2880" allowCustomSqlDatabase="true"
    sqlConnectionString="data source=mydatasource;initial catalog=mydb;user id=userid;password=password;
integrated security=False;MultipleActiveResultSets=True;"
cookieless="true" />

<authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" protection="All" cookieless="UseCookies"/>
    </authentication>

Screenshot of my database


Solution

  • Ok, I finally found out what was the real problem. I had random generator for machineKey, and when the IIS was restarted new machineKey was generated so I couldnt decrypt the auth cookie. The solution is simple -> use static key if you have low level security site