Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.net中从HttpContext检测https?_C#_.net - Fatal编程技术网

C# 在.net中从HttpContext检测https?

C# 在.net中从HttpContext检测https?,c#,.net,C#,.net,我继承了一堆包含大量硬编码路径的web应用程序代码。我的任务是尝试使用https://运行它 除了在URL中检测“https://”之外,是否还有一种更内置的方法来检测当前上下文是https 寻找类似于: System.Web.HttpContext.Current.Request.Url.Protocol.ToString()您可以使用HttpContext.Current.Request.isSecureReconnection直接回答您的请求,例如System.Web.HttpContex

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

除了在URL中检测“https://”之外,是否还有一种更内置的方法来检测当前上下文是https

寻找类似于:


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

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

直接回答您的请求,例如
System.Web.HttpContext.Current.Request.Url.protoString()
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

可能是“.Equals(False)”位。虽然它应该可以工作,但是用这种方式编写代码没有任何意义。另外,在VB中不应该使用+进行字符串连接。为了使代码更易于阅读,如果不是HttpContext.Current.Request.IsSecureConnection,我会这样写