简体   繁体   English

带有URL重写的HTTPS无法在Appharbor上运行

[英]HTTPS with URL rewriting is not working on appharbor

I have built an application on asp.net 3.5 which is hosted on AppHarbor . 我在AppHarbor上托管的asp.net 3.5上构建了一个应用程序。 The problem is that on HTTPS URL rewriting is not working. 问题是在HTTPS上URL重写不起作用。 The following is the code to run some of the pages on SSL: 以下是在SSL上运行某些页面的代码:

string CurrentUrl = Request.Url.ToString();
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;      
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;       
string sDir = oInfo.Directory.Name;     
pageName = sRet;
if (sRet == "Register.aspx" || sRet == "Login.aspx" || sRet == "Post.aspx" || sRet == "ChangePassword.aspx" || sRet == "ChangeUserStatus.aspx" || sRet == "Verification.aspx" || sRet == "ContactInfo.aspx" || sRet == "Find.aspx" || sRet == "MyAccount.aspx" || sRet == "MyEmailAddresses.aspx" || sRet == "Load.aspx" || sRet == "MyPostedLoads.aspx" || sRet == "MySubmittedBids.aspx" || sRet == "MySavedAddresses.aspx" || sRet == "MyCarriers.aspx" || sRet == "MyPotentialLoads.aspx" || sRet == "MyFreightAlarms.aspx" || sRet == "MyFreightAlarmsPreferences.aspx" || sRet == "MyAddress.aspx" || sRet == "GetUserComments.aspx" || sRet == "MyCreditCard.aspx" || sRet == "MyWallet.aspx" || sRet == "InvoiceMe.aspx" || sRet == "MyShippers.aspx" || sRet == "MyCoWorkers.aspx" || sRet == "MyACH.aspx" || sRet == "RouteMap.aspx" || sRet == "Pricing.aspx" || sRet == "PricingPayment.aspx" || sRet == "PaymentProcessed.aspx")
{
    string NewUrl = "";

    if (!Request.IsSecureConnection && !string.Equals(HttpContext.Current.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.OrdinalIgnoreCase))
    {
        NewUrl = Regex.Replace(CurrentUrl,
                               @"^https?(://[^/:]*)(:\d*)?", 
                               "https$1", 
                               RegexOptions.IgnoreCase);

        Response.Redirect(NewUrl);
    }
}

And the rule for URL rewrite on web.config: URL的规则在web.config上重写:

<rewrite>
  <rules>
    <rule name="Rewrite with .aspx" stopProcessing="true">
      <match url="^([^\.]+)$" />
      <action type="Rewrite" url="{R:1}.aspx" />
    </rule>
    <rule name="Redirect .aspx page requests" stopProcessing="true">
      <match url="(.+)\.aspx" />
      <action type="Redirect" url="{R:1}" />
    </rule>
  </rules>
</rewrite>

The problem is the that page remains in an indefinite loop and can not redirect properly. 问题在于该页面处于不确定循环中,无法正确重定向。

RequireHttpsAttribute: If you're using the built-in RequireHttpsAttribute to ensure that a controller action always uses HTTPS you will experience a redirect loop. RequireHttpsAttribute:如果您使用内置的RequireHttpsAttribute来确保控制器操作始终使用HTTPS,则会遇到重定向循环。 The reason is that SSL is terminated at the load balancer level and RequireHttps doesn't recognize the X-Forwarded-Proto header it uses to indicate that the request was made using HTTPS. 原因是SSL在负载均衡器级别终止,并且RequireHttps无法识别用于指示请求是使用HTTPS进行的X-Forwarded-Proto标头。

The "meat" is in bold. “肉”以粗体显示。

See: AppHarbor SSL FAQ - specifically the Troubleshooting section. 请参阅: AppHarbor SSL常见问题解答 -特别是“ 疑难解答”部分。

It's the same issue if you have an SSL concentrator or similar device in front of your web server(s). 如果您的Web服务器前有一个SSL集中器或类似的设备,则会出现同样的问题。 It's quite common in "cloud hosting" environments.... 在“云托管”环境中非常普遍。

Hth.... 嗯...

Like EdSF mentioned, the problem you are experiencing is because the SSL (HTTPs) is at the load-balancer level. 就像提到的EdSF一样,您遇到的问题是因为SSL(HTTP)位于负载均衡器级别。 Meaning, all requests that come into your ASP.NET application will be HTTP. 意思是,进入ASP.NET应用程序的所有请求都是HTTP。

So in your application running on AppHarbor, the following will always be true: 因此,在运行于AppHarbor的应用程序中,以下条件将始终成立:

Request : https://mysite.com/about
---------------------------------------------------------------------------------
-> Request.Url.Scheme // http
-> Request.Url.AbsoluteUri // http://mysite.com:port/about
-> Request.issecure // false

You rewrite rules are relying on the protocal/scheme to be https and it never will be, causing an infinite loop. 您重写规则依赖于协议/方案为https ,而永远不会如此,从而导致无限循环。

The way to check for HTTPS in your ASP.NET application running on AppHarbor would be the following: 在AppHarbor上运行的ASP.NET应用程序中检查HTTPS的方法如下:

string.Equals(Request.Headers["X-Forwarded-Proto"], 
              "https", 
              StringComparison.InvariantCultureIgnoreCase);

I also host my web application on AppHarbor and needed a better solution so I created the SecurePages project ( NuGet - GitHub ). 我还将Web应用程序托管在AppHarbor上,并且需要更好的解决方案,因此我创建了SecurePages项目( NuGet - GitHub )。 This project allows you to configure secure/https URLs with string literals as well as regex. 该项目允许您使用字符串文字和正则表达式来配​​置安全/ https URL。 It also forces all other URLs to use HTTP. 它还会强制所有其他URL使用HTTP。 You can also register custom predicate delegates that act as HTTPs request matching rules. 您还可以注册充当HTTP请求匹配规则的自定义谓词委托。 So you could register one for AppHarbor to check the headers: 因此,您可以为AppHarbor注册一个以检查标头:

//Secure a page    
secureUrls.AddUrl("/Register.aspx");

//Secure any page under /cart/*
secureUrls.AddRegex(@"(.*)cart", RegexOptions.IgnoreCase);

//Register a custom HTTPs match rule for AppHarbor
SecurePagesConfiguration.RegisterCustomMatchRule(
                c => string.Equals(c.Request.Headers["X-Forwarded-Proto"], "https", StringComparison.InvariantCultureIgnoreCase));

Secure pages also supports Unit Testing and local browser testing with IIS Express. 安全页面还通过IIS Express支持单元测试和本地浏览器测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM