HTTPS网址无效 [英] HTTPS url not working
问题描述
您好,
我正面临着HTTPS网址的重定向问题。我在服务器上安装了SSL证书。请在下面找到我的重定向代码。我已将它放在global.asax文件的Application_BeginRequest事件中。
如果 不 HttpContext.Current.Request.Url.AbsoluteUri.Contains( https://)然后
如果 ConfigurationManager。 AppSettings( UseSecure)。ToLower()= true 然后
昏暗 securePages 作为 字符串 = ConfigurationManager.AppSettings( SecurePages)
Dim securePagesList 作为 字符串()= securePages.Split( ,)
Dim IsSSLPage = False
对于 每个页在 securePagesList
如果 strUrl.IndexOf(page)<> -1 然后
如果 strUrl.IndexOf( .aspx)> 0 和 HttpContext.Current.Request.ServerVariables( HTTPS)= off 然后
IsSSLPage = True
退出 对于
结束 如果
结束 如果
下一步
Common.RedirectWithSSL(IsSSLPage)
结束 如果
结束 如果
公共 共享 Sub RedirectWithSSL( ByVal isSSL 作为 布尔)
如果 HttpContext.Current.Request.HttpMethod.ToLower()= post 然后
返回
结束 如果
Dim httpurl 作为 [ String ] = System.Web.HttpContext.Current.Request.Url.ToString()
Dim checkUrl 作为 String = httpurl.ToLower()
如果 isSSL httpurl .ToLower()。StartsWith( http://) AndAlso 不 httpurl.ToLower()。StartsWith( https://)然后
httpurl = httpurl.Replace( http, https)。ToString()
System.Web.HttpContext.Current.Response.Redirect(httpurl)
ElseIf 不 isSSL 和 checkUrl.StartsWith( https:// )然后
httpurl = httpurl.Replace( https, http)。ToString()
System.Web.HttpContext.Current.Response.Redirect(httpurl)
结束 如果
结束 Sub
我已将我的安全页面名称保存在web.config文件中,逗号分隔,如checkout,meber,orderhistory等。
现在,当我检查了URL,如https://mysite.com/member/,它通过重定向循环触发302状态。虽然我用https://mysite.com/member.aspx检查它,但它的工作正常。
我无法弄清楚问题。这只是服务器上的问题。在我的开发机器中它工作得很好。 (我已经在 http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx [ ^ ]
最后花了15个小时后,我终于找到了解决方案。:omg:
问题是我的托管公司在他们的服务器上使用负载均衡器,所以If Not HttpContext.Current.Request.Url.AbsoluteUri.Contains( https://)永远不会被设置为true。因此,对于每个请求,它都会转到该函数并发生重定向循环。
在负载均衡环境中,SSL连接在负载平衡器处停止,因此您永远不会使用标准方法进行SSL连接,例如:
- HttpContext.Current.Request.IsSecureConnection
- HttpContext.Current.Request.Url.Scheme
- Request.ServerVariables(HTTPS)
- HttpContext.Current.Request.Url.AbsoluteUri.Contains(https://)
在负载平衡环境中,自定义请求将插入到标头中,以帮助识别安全请求。假设您的主机已在其负载均衡器上设置此变量,则其名称可以是任何名称。在我的托管服务器的情况下,它似乎是HTTP_CLUSTER_HTTPS。 X |
我希望这个解决方案可以节省15个小时。 :)
Hello,
I am facing redirection issue for the HTTPS url. I have SSL certificate installed at the server. Please find my redirection code below. I have placed it at Application_BeginRequest event of global.asax file.
If Not HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://") Then
If ConfigurationManager.AppSettings("UseSecure").ToLower() = "true" Then
Dim securePages As String = ConfigurationManager.AppSettings("SecurePages")
Dim securePagesList As String() = securePages.Split(",")
Dim IsSSLPage = False
For Each page In securePagesList
If strUrl.IndexOf(page) <> -1 Then
If strUrl.IndexOf(".aspx") > 0 And HttpContext.Current.Request.ServerVariables("HTTPS") = "off" Then
IsSSLPage = True
Exit For
End If
End If
Next
Common.RedirectWithSSL(IsSSLPage)
End If
End If
Public Shared Sub RedirectWithSSL(ByVal isSSL As Boolean)
If HttpContext.Current.Request.HttpMethod.ToLower() = "post" Then
Return
End If
Dim httpurl As [String] = System.Web.HttpContext.Current.Request.Url.ToString()
Dim checkUrl As String = httpurl.ToLower()
If isSSL And httpurl.ToLower().StartsWith("http://") AndAlso Not httpurl.ToLower().StartsWith("https://") Then
httpurl = httpurl.Replace("http", "https").ToString()
System.Web.HttpContext.Current.Response.Redirect(httpurl)
ElseIf Not isSSL And checkUrl.StartsWith("https://") Then
httpurl = httpurl.Replace("https", "http").ToString()
System.Web.HttpContext.Current.Response.Redirect(httpurl)
End If
End Sub
I have saved my secure page name in web.config file with comma separated like checkout,meber,orderhistory etc.
Now, when I check the URL like https://mysite.com/member/ it fires 302 status with redirecting loop. While I check it with https://mysite.com/member.aspx it''s working just fine.
I cannot figure out the issue. It is only issue at server. In my development machine it is working just fine. (I have installed self signed certificate with the help of http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx[^]
Finally after spending 15 hours on this, I finally found the solution. :omg:
The issue is my hosting company use a load balancer on their server so the If Not HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://") is never got set to true. So for every request it goes to that function and redirection loop occurs.
In a Load Balanced environment the SSL connection stops at the load balancer so that you can never test for an SSL connection using standard methods such as:
- HttpContext.Current.Request.IsSecureConnection
- HttpContext.Current.Request.Url.Scheme
- Request.ServerVariables("HTTPS")
- HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://")
In a load balanced environment a custom request gets inserted into the header to assist in identifying a secure request. Assuming that your host has setup this variable on their load balancer its name could be anything. In the case of my hosting server it appears to be "HTTP_CLUSTER_HTTPS". X|
I hope this solution will save some one else 15 hours. :)
这篇关于HTTPS网址无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!