3

We've just discovered a problem with our new "cloud" hosting. Basicially NAT/port forward has been configured because they won't allow multiple internal IPs so that the SSL certificates can be done for the multiple sites.

This causes a problem with any MVC route that gets generated where the protocol is also specified. Also if an attempt is made to go to a URL that requires a login the ReturnUrl param in the redirected https login URL also has the port number in.

The question is - is there any way to tell MVC to never include a port number even if one is present, short of using the UrlHelpers action/route method overloads that allow you to specify the domain and hence manually strip off the port number.

In case it's not obvious from the above what the request process is like, here's a little example:

  • Users browser makes request to site
  • Server forwards same URL but with a port number attached
  • Page is generated
  • Response sent back to users browser.
  • User logs in but now the return URL has the port in so when it redirects the user gets page cannot be displayed.

Due to the port forwarding malarkey this seems to cause MVCs routing to become confused. Not knowing the technical server side so well I'm not sure whether it is just the hosting company not configuring something correctly in IIS or the NAT stuff or whether there is simply some web.config stuff that needs configuring to enable MVC to generate the correct URLs as if this port malarkey doesn't exist.

Any tips or pointers most appreciated.

2
  • Ugh, if you have client script, that too will be a headache. Maybe the folks over at Server Fault can help on the config side..for my part, I don't get why the port is appended in the first place - is it to "toggle" 443 and 80 (http/s)?
    – EdSF
    Sep 1, 2014 at 16:45
  • No it's because you have a single server hosting multiple sites but for the purposes of SSL certificates it wants an IP address and you can't have the same IP address registered for multiple domains. Because there is only 1 internal IP allowed on the cloud server then forwarding with a port number to differentiate was the only way but this caused the problems mentioned. However see my own answer - phew!
    – Peter
    Sep 3, 2014 at 14:25

1 Answer 1

8

Luckily for me it turns out there is a configuration solution (because people were asking "what about a code change?" -> Me: "Seriously? NO this must be a config issue").

The solution is simple to implement; add the following to the <appSettings> section of the web.config:

<add key="aspnet:UseHostHeaderForRequestUrl" value="true" />

Thus when ASP.NET/MVC generates URLs it will always use the host header for the website upon which the users request was made instead of the port-forwarded version, yay!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.