I am therefore trying to get the session info storing on the sql server database instead, but I just can't get it to work.
I have setup the ASPState database using the aspnet_regsql.exe, and both servers seem to store their data to this database. But the problem is that they both seem to have their own sessions and don't share them.
I knocked up a demo page that stores some data to session then retrieves it from session. I can store the data and retrieve it on http://server1/sessionSharing/Default.aspx, if I then switch to http://server2/sessionSharing/Default.aspx and try to retrieve the data from session it blows up saying there isn't any.
After much googling it sounded like adding a machine key to my config would solve this problem but it doesn't make any difference. I've tried it in web.config and machine.config but still no good. I'm starting to wonder if I have missed something very fundamental here. Should I be setting some sort of cookie manually perhaps or does this happen automatically? My browser doesn't seem to be prompting me to accept or reject cookies on my demo site even though it does on other sites. Do I have to enable forms authentication or something? I noticed that the session ID that I am displaying on the page seems to change with each postback, is that right? It works OK if I put the session ID in the url but I don't like this solution as it causes problems with people bookmarking etc.
All I have done so far is as follows (keys have been redacted so may not work like this).
web.config
Expand|Select|Wrap|Line Numbers
- <sessionState mode="SQLServer" sqlConnectionString="Data Source=server1;User ID=test;Password=test"></sessionState>
- <machineKey validationKey="xxxxxxxxA5EC9214B0063AEFC8E0354998F393D81D1D3AD7551C792DF00ADA736B410E71C1791B70AE925AFE15BFB5FBF4EFAF2E4A55008FA06F31D8xxxxxxxx" decryptionKey="xxxxxxxx8DFA128AB16A1985C48F2491DBEF5744xxxxxxxx" validation="SHA1" />
- <authentication mode="Windows"/>
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- Label2.Text = HttpContext.Current.Session.SessionID;
- Label1.Text = ConfigurationManager.AppSettings["ServerName"];
- }
- protected void btnStore_Click(object sender, EventArgs e)
- {
- Session["Data"] = TextBox1.Text;
- }
- protected void btnRetrieve_Click(object sender, EventArgs e)
- {
- TextBox2.Text = Session["Data"].ToString();
- }
Ben.