Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't Request.Url.Scheme returning HTTPS?

I am working on a local server and I need a specific URL to be accessed through HTTPS while the rest through HTTP. I have configured Visual Studio to use IIS Express so I can use HTTP/SSL.

I have a method like so:

[RequireHttps]
public ActionResult SomeHttpsMethod()
{
     //Do something
}

In another place I have:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", new { someParams }, Request.Url.Scheme);

If I access my site using HTTP i.e. http://localhost:httpport, I still get HTTP returned from Request.Url.Scheme instead of HTTPS. Is that how it is meant to work?

Obviously if I accesss my site using HTTPS i.e. to begin with i.e. https://localhost:sslport, HTTPS is returned (which is what I want) but I don't want to have to access the site in HTTPS, only for that particular URL/controller method.

like image 600
Kevin Lee Avatar asked Sep 04 '14 14:09

Kevin Lee


2 Answers

This line:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", 
    new { someParams }, Request.Url.Scheme);

constructs a URL based on your current request's scheme, which is HTTP. Request always refers to the current request.

You'd be better off hard-coding "https" in this place since you always want it to be secure anyway:

var url = Url.Action("SomeHttpsMethod", "SomeHttpsController", 
    new { someParams }, "https");
like image 197
Haney Avatar answered Oct 06 '22 14:10

Haney


In my case it was down to badly set up rules on the ARR server. Just something to look at if you get HTTP rather than HTTPS.

like image 40
Peter Avatar answered Oct 06 '22 13:10

Peter