none
Session and cookies not working on cloud

    Question

  • Hi,

     

    I have migrated a website to cloud, which works perfectly on my local machine using the Compute and Storage Emulators.

    However, when I put it onto Windows Azure Cloud, the session and cookies are not working fine. Part of web.config file is given below.

        <sessionState timeout="30"
                      mode="SQLServer"
                      sqlConnectionString="Server=sxxxxxxxxx.database.windows.net;User ID=xxxxx@sxxxxxxxxx;Password=MY_PASSWORD"
                      cookieless="false"
                      allowCustomSqlDatabase="true" >
        </sessionState>
      </system.web>

    Can anyone please guide be in correcting the same ? All the other things in the website are pretty much working fine.

     

    Thanks

    Sandeep

     

    Friday, July 29, 2011 3:41 AM

Answers

All replies

  • FYI, this is also posted on StackOverflow here: http://stackoverflow.com/questions/6868461/session-and-cookies-not-working-on-windows-azure-cloud-environment.

    Could you describe more of what "not working" means? Is there some error message? Or some unexpected behavior?

    • Marked as answer by Wenchao Zeng Friday, August 05, 2011 6:49 AM
    Friday, July 29, 2011 4:33 AM
  • @Steve,

    The website was working fine in general without the sessionstate code, i.e, without

    <sessionState timeout="30"
                      mode="SQLServer"
                      sqlConnectionString="Server=sxxxxxxxxx.database.windows.net;User ID=xxxxx@sxxxxxxxxx;Password=MY_PASSWORD"
                      cookieless="false"
                      allowCustomSqlDatabase="true" >
        </sessionState>

    The only problem was that the website was working without recognizing the cookies and there were UNEXPECTED behavior.

    So I added the above code. After adding, the whole website stopped working and the following error took birth on all the pages that use cookies.

    Server Error in '/' Application.

    Runtime Error

     Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

    Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

    <!-- Web.Config Configuration File -->
    
    
    
    <configuration>
    
     <system.web>
    
      <customErrors mode="Off"/>
    
     </system.web>
    
    </configuration>

    Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

    <!-- Web.Config Configuration File -->
    
    
    
    <configuration>
    
     <system.web>
    
      <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    
     </system.web>
    
    </configuration>

     

    Is the sessionState code written by me CORRECT ?????? or should I make any change in that?

    Is there anything else that I have to do for the Session and Cookies to work ???

     

    Regards

    Sandeep

     


    Friday, July 29, 2011 6:00 AM
  • @Steve,

    BTW, I am the same person who has posted this problem on

     http://stackoverflow.com/questions/6868461/session-and-cookies-not-working-on-windows-azure-cloud-environment.

    -Sandeep

    Friday, July 29, 2011 6:03 AM
  • "BTW, I am the same person who has posted this problem on ..." Yes, that's what I was assuming. I put the FYI here (and on StackOverflow) so that people thinking about answering could check the other place and see if it had already been answered elsewhere.

    Without knowing the error message, it will be hard to guess at the issue. I would suggest you go ahead and turn custom errors off as suggested or RDP into one of the VMs and hit the web site there so you can get the exception text and a stack trace.

    Friday, July 29, 2011 6:09 AM
  • @Steve
    Thanks for the quick reply.

    I got to know that I have to create a database on Azure. This is what I get after I create the AspState database / tables on SQL Azure using a modified scripts that were provided by one of the blogs.
    All the scripts ran successfully and the required database and its corresponding tables were created.

     

    Server Error in '/' Application.

    Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above.

    Source Error:

     

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace:

    [HttpException (0x80004005): Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above.]
    System.Web.SessionState.SqlPartitionInfo.GetServerSupportOptions(SqlConnection sqlConnection) +3597220
    System.Web.SessionState.SqlPartitionInfo.InitSqlInfo(SqlConnection sqlConnection) +99
    System.Web.SessionState.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo, TimeSpan retryInterval) +3598418
    System.Web.SessionState.SqlSessionStateStore.GetConnection(String id, Boolean& usePooling) +396
    System.Web.SessionState.SqlSessionStateStore.SetAndReleaseItemExclusive(HttpContext context, String id, SessionStateStoreData item, Object lockId, Boolean newItem) +362
    System.Web.SessionState.SessionStateModule.OnReleaseState(Object source, EventArgs eventArgs) +929
    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +266

     

    Is there anything else that I have to do apart from what is given in the blog.
    FYI: I got the same error on the local dev env also.

     

    So to summarize, I have done the following.

    1. Migrated a website to Windows Azure successfully

    2. Created the required connection strings for the database access successfully.

    3. Tested the website on the local machine using Compute Emulator and Storage Emulator.

    4. When I published the website and uploaded the packageFile and configurationFile, everything was working fine in general. BUT

    for the pages which used session/cookies. All static pages worked well. All database access were proper.

    5. Then, I wrote some code for setting right the session.

    <sessionState timeout="30"
                      mode="SQLServer"
                      sqlConnectionString="Server=sxxxxxxxxx.database.windows.net;Database=abcd;User ID=xxxxx@sxxxxxxxxx;Password=MY_PASSWORD"
                      cookieless="false"
                      allowCustomSqlDatabase="true" >
        </sessionState>
      </system.web>

    GOT ERRORS AND ENTIRE WEBSITE STOPPED WORKING

    6. Created the database and tables that were required (ASPSTATE database and its corresponding tables). Everything went well.

    http://blogs.msdn.com/b/sqlazure/archive/2010/08/04/10046103.aspx

    Tested the website ... Still getting the error on local and on cloud...as mentioned previously in this post.

    Can anyone please guide me to rectify this.

     

    Regards,

    Sandeep


    Friday, July 29, 2011 11:15 AM
  • Storing session state in SQL Azure is not officially supported by Microsoft. I'd like suggest you use AppFabric Caching instead as it is officially supported.

    Configuring an ASP.NET Session State Provider (Windows Server AppFabric Caching)
    http://msdn.microsoft.com/en-us/library/ee790859.aspx 

    Monday, August 01, 2011 10:04 AM
  • I experience the similar error message before. In my case, the problem lies on the username that we use to connect to SQL Azure has limited permission (which may not have permission to execute SP, tables, view, etc.).

    For testing purpose, I simply used server admin and it works.

    Of course, eventually I am not recommending you to use server admin, but you'll need to grant necessary access on objects to the user.

    Have a try and see if this works for you.


    regards, wely
    Tuesday, August 02, 2011 9:31 AM
  • You can't use session state, but you can use the FormsAuthenticationTicket that works just fine in the cloud. I use this in conjunction with azure storage tables to hold more information, but technically you can cram everything you need into the string UserData delimited by | or ^ arguably. Here's a sample snippet. I think you can run with it from here.

     #region CookieTicket Production Method -- write out formsauthentication ticket session cookie
                //set cookie
                var authTicket = new FormsAuthenticationTicket(1,
                                                               TokenGuid,
                                                               DateTime.Now,
                                                               DateTime.Now.AddMinutes(14*60), //14 hours if you please.
                                                               true,
                                                               model.LogonEmailAddress.ToString());

                string cookieContents = FormsAuthentication.Encrypt(authTicket);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieContents)
                {
                    Expires = authTicket.Expiration,
                    Path = FormsAuthentication.FormsCookiePath

                };
                Response.Cookies.Add(cookie);
               
                #endregion


    Monday, September 10, 2012 7:05 PM
  • The Dedicated, distributed in-memory "caching preview" feature that's available with the 1.7 SDK also makes ths old thread a bit of a mute point. In about 5 minutes, you can create a session enabled cloud service that isn't dependent on the Caching service (which has throttle limits) and are willing to give up a bit of CPU/RAM to help power it.
    Monday, September 10, 2012 9:04 PM
    Moderator