I have a web application hosted on multiple servers some of which are on https. How can I check from code behind if a page is currently in http or https?
Asked
Active
Viewed 4.2k times
7 Answers
86
You can refer to the Request.IsSecureConnection
property on the HttpRequest
class. For a full reference outside a page, user control or alike, use HttpContext.Current.Request.IsSecureConnection
.

Troels Thomsen
- 11,361
- 4
- 28
- 29
-
8Beware- IsSecureConnection can give false negatives. http://stackoverflow.com/questions/998397/why-does-request-issecureconnection-return-false-when-true-is-expected – Jude Allred Mar 28 '11 at 21:13
-
2Answer is little old, so now, for me it was a bit changed with owin. I got the value by "System.Web.HttpContext.Current.GetOwinContext().Request.IsSecure". May be this comes handy for some one later. :) – MGR May 11 '16 at 11:11
29
Page.Request.Url.Scheme
works as well. It returns http
, https
, etc.
Ref: http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx
8
Update for Aspnet Core 2.0, now, you should use Request.IsHttps
inside your controllers.

Julien Leicher
- 81
- 1
- 2
-
As of NET Core 2.2 you can use ```Request.Scheme``` which should return either ```https``` or ```http```. Either work fine on Windows servers although on Ubuntu ```IsHttps``` and ```Request.Scheme``` both say the request is not secure for me so not sure if this is a bug or something configured wrongly. – Robin Wilson Jan 04 '19 at 23:54
4
Alternatively:
Request.ServerVariables["SERVER_PROTOCOL"];

Mr. Smith
- 5,489
- 11
- 44
- 60
-
1This returns `HTTP/1.1` for me on both http and https while `Request.IsSecureConnection` returns as expected. – atheaos Jan 27 '16 at 15:27
1
Try this,
aCookie.Secure = HttpContext.Current.Request.IsSecureConnection

Andro Selva
- 53,910
- 52
- 193
- 240

Eric Franklin
- 11
- 1