Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net check if page is http or https

Tags:

asp.net

https

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?

like image 631
Drahcir Avatar asked Aug 31 '09 08:08

Drahcir


People also ask

How do I know if a website is http or https?

Fortunately, there are two quick checks to help you be certain: Look at the uniform resource locator (URL) of the website. A secure URL should begin with “https” rather than “http.” The “s” in “https” stands for secure, which indicates that the site is using a Secure Sockets Layer (SSL) Certificate.

How check URL is secure or not in C#?

To check if a Url is valid in C# you can use Uri. TryCreate() method which creates a new Uri and it does not throw an exception if the Uri cannot be created, it will return a bool if it was created successfully.

What is the use of UseHttpsRedirection?

HTTPS Redirection Middleware (UseHttpsRedirection) to redirect HTTP requests to HTTPS. HSTS Middleware (UseHsts) to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.

What is HSTS in ASP net Core?

Per OWASP, HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that's specified by a web app through the use of a response header. When a browser that supports HSTS receives this header: The browser stores configuration for the domain that prevents sending any communication over HTTP.


2 Answers

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.

like image 169
Troels Thomsen Avatar answered Sep 23 '22 17:09

Troels Thomsen


Page.Request.Url.Scheme 

works as well. It returns http, https, etc.

Ref: http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx

like image 42
Ankur-m Avatar answered Sep 23 '22 17:09

Ankur-m