从.net中的HttpContext检测https?

我inheritance了一堆Web应用程序的代码,其中包含大量硬编码路径。 我的任务是尝试使用https://运行它。

除了在URL中检测“https://”之外,是否有更多的带内方式来检测当前上下文是否为https?

寻找类似的东西:

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

您可以使用HttpContext.Current.Request.IsSecureConnection

System.Web.HttpContext.Current.Request.Url.Protocol.ToString()类的请求的直接答案是System.Web.HttpContext.Current.Request.Url.Scheme ,尽管正如Brandon所说,在他的回答中 HttpContext.Current.Request.IsSecureConnection是检测使用https作为布尔值的方法,可能更适合您在问题中提出的问题。

我使用此代码自动重定向到https

 If HttpContext.Current.Request.IsSecureConnection.Equals(False) Then Response.Redirect("https://" + Request.ServerVariables("HTTP_HOST") & HttpContext.Current.Request.RawUrl) End If