从.net中的HttpContext检测https?

时间:2010-10-04 14:40:19

标签: c# .net

我为一个包含大量硬编码路径的网络应用程序继承了一堆代码。我的任务是尝试使用https://运行它。

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

寻找类似的东西:

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

3 个答案:

答案 0 :(得分:46)

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

答案 1 :(得分:13)

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

答案 2 :(得分:2)

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

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