Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting https from HttpContext in .net?

Tags:

I've inherited a pile of code for a web app which contains oodles of hard-coded paths. I've been tasked with trying to get it to run using https://.

Other than detecting "https://" in the URL is there a more inband way to detect that the current context is https?

Looking for something like:

System.Web.HttpContext.Current.Request.Url.Protocol.ToString()

like image 434
Dave K Avatar asked Oct 04 '10 14:10

Dave K


People also ask

Is HttpContext current thread safe?

HttpContext object is not thread-safe.

What is HttpContext current in C#?

This property is a static property of the HttpContext class. The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class. You can also use the Page.

What is HttpContext .NET Core?

For more information and example code, see ASP.NET Core Blazor Server additional security scenarios. HttpContext encapsulates all information about an individual HTTP request and response. An HttpContext instance is initialized when an HTTP request is received.


2 Answers

You can use HttpContext.Current.Request.IsSecureConnection

like image 68
Brandon Avatar answered Nov 11 '22 18:11

Brandon


The direct answer to your request for something like System.Web.HttpContext.Current.Request.Url.Protocol.ToString() is System.Web.HttpContext.Current.Request.Url.Scheme, though as Brandon says, in his answerHttpContext.Current.Request.IsSecureConnection is the way to detect use of https as a boolean and likely more appropriate to the problem you give in your question.

like image 29
Jon Hanna Avatar answered Nov 11 '22 17:11

Jon Hanna