Kevin Lee
Kevin Lee

Reputation: 1135

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.

Upvotes: 7

Views: 33719

Answers (2)

Peter
Peter

Reputation: 21

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.

Upvotes: 1

Haney
Haney

Reputation: 34882

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");

Upvotes: 9

Related Questions