2

Well, I finally had to create an account here. Been using this for years and have often found my answer here, but not this time. Well, I actually have found a lot of people with similar problems, but none of their solutions have helped me.

I have started on a new MVC3 project, so it's quite simple so far. I've made a handful before, so I kinda know what I'm doing (but not quite, obviously, why else be here ;-)

My problem is apparently a fairly common one: A request starts a new Session, even though the user already has one.

The most frustrating part of this is, it works perfectly on my hosted service, but is broken on localhost.

I have done a number of things to solve this:

  • There is no underscore in my computer's name.
  • The Session contains custom data (the error only occurs after user has logged in).
  • I have added the following to web.config (hmpf, guess you'll have to assume the gt / lt chars):
    httpProtocol
      customHeaders
        clear /
        add name="Access-Control-Allow-Origin" value="*" /
      /customHeaders
    /httpProtocol
  • and this too:

    modules runAllManagedModulesForAllRequests="false"/
  • With InProc sessionstate, I have tried with 'cookieless' both true and false.

  • My hosts file contains nothing about localhost.

hm. Looking at this list I'm sure I've left some out. Some on purpose too, as they were hopeless (yes, even more than the above), and born from desperation.

As mentioned this is particularly unnerving as it works on my host - could there be some configuration settings I need to tweak on the dev server (VS2010)?

I've been working from the premise that the issue is due to cross-domain security (it thinks I'm coming from another domain).

The fail happens on this request:

url: 'http://localhost:50396/moody/changeBuilding/' + elem.selectedIndex,  

It's part of the options array I use with the jQuery.ajax function.

I change the domain when uploading to the host, but only the part localhost:port, everything else in the application is identical.

I've been banging my head against this for 2 days now, and will miss my exam :-( I'm determined to bury this 6 feet under, though.

I would be very grateful for any and all suggestions!

Tobias
  • 37
  • 7

2 Answers2

0

Have you tried using something like fiddler to make sure that the session cookie is being sent in the AJAX request. It should be sent if the domain is the same but it's work checking.

Edit: This SO post on changing ports is worth reading too.

Edit: Given the new information in Charlino's comments (and the sterling detective work carried out therein) if the problem is only on your local dev machine then the easiest way to work around your localhost/127.0.0.1 issue is by manually changing the browser url from 127.0.0.1:50396 to localhost:50396, logging in again to get the new cookie, then you are good to go.

Community
  • 1
  • 1
Neil Thompson
  • 6,356
  • 2
  • 30
  • 53
  • Yes, I have been using fiddler extensively. The request I send looks like this:GET http://127.0.0.1:50396/moody/changeBuilding/1 HTTP/1.1 Host: 127.0.0.1:50396 User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 Accept: application/xml, text/xml, */*; q=0.01 Origin: http://localhost:50396 Referer: http://localhost:50396/Moody/Home/9 Accept-Language: da-DK Accept-Encoding: gzip, deflate Connection: keep-alive Connection: keep-alive
    Of course, this means that my url for the options array really looks like ... the following:
    – Tobias Dec 12 '12 at 22:12
  • [continued] url: 'http://ipv4.fiddler:50396/moody/changeBuilding/' + elem.selectedIndex, Sorry for the wall of text, but I really have no clue how to make line breaks, or why stackoverflow would make us suffer through this horrid comment system. – Tobias Dec 12 '12 at 22:13
  • _changing the browser url from 127.0.0.1:50396 to localhost:50396_ --- I've tried that to no avail. Typing in 'localhost:50396', '127.0.0.1:50396' and 'ivp4.fiddler' yields the same responces. – Tobias Dec 13 '12 at 23:04
0

I change the domain when uploading to the host, but only the part localhost:port, everything else in the application is identical.

Reading the above, I image the session cookie isn't being sent because you're changing domains.

Let's sit back and think about how sessions work. Basically ASP.NET contains a collection of sessions and their data. When each request comes in, ASP.NET must map that request to an existing session OR create a new session for them.

So how does ASP.NET know what session belonged to each incoming request? Or know that it needs to create a new request? The only way to know this is if the request contained some information, a 'key', which told ASP.NET what session to give the request... or in the absence of this 'key', create a new session.

How does the request send this 'key'? Through cookies.

So therefore, if you change the domain, the cookies isn't going to be sent... so therefore, ASP.NET will create a new session for the request.

Charlino
  • 15,802
  • 3
  • 58
  • 74
  • The cookie is there. I can see it in safari's dev panel. But it does write an error it hasn't before: XMLHttpRequest cannot load http://ipv4.fiddler:50396/moody/changeBuilding/1. Origin http://localhost:50396 is not allowed by Access-Control-Allow-Origin. Which is weird, I think, since I should have allowed everyone and -thing to access (as per my question). – Tobias Dec 12 '12 at 22:26
  • Cookies between hostnames and ip addresses aren't shared. E.g. `http://localhost:50396` does not share cookies with `http://127.0.0.1:50396` ... so therefore, they do not share sessions. – Charlino Dec 12 '12 at 23:20
  • Ok then, so I need to do something with my hosts file after all? I have tried a thing or two, but to no avail. Please will you elaborate? – Tobias Dec 13 '12 at 03:42
  • Possibly. Firstly though, why do you need to change between the two? Can you not just stick with one method? – Charlino Dec 13 '12 at 07:52
  • I can't debug on the host, and I can't debug at home if 'localhost' doesn't work. Thanks for taking the time! :-) – Tobias Dec 13 '12 at 23:00