2016-02-17 101 views
9

我在同一個IIS服務器上設置了兩個IIS網站。網站A包含大部分內容,這是用戶在請求我們的地址時顯示的內容(例如www.websitea.com)。網站B是一個單獨的項目,只包含整個內容的一部分,因此它是內部的(在IIS中綁定到websiteb.com),但通過URL重寫,用戶可以通過鍵入www.websitea.com/websiteb 。嘗試將HTTP重定向到HTTPS(C#,IIS),但HTTPS在響應頭中回到HTTP - 重定向循環

URL重寫看起來像這樣的一個網站的web.config:

<rewrite> 
    <rules> 
    <clear /> 
    <rule name="Website B rewrite rule" stopProcessing="true"> 
     <match url="^websiteb(.*)" /> 
     <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> 
     <add input="{CACHE_URL}" pattern="^(https?)://" /> 
     </conditions> 
     <action type="Rewrite" url="{C:1}://websiteb.com{R:1}" /> 
    </rule> 
    </rules> 
</rewrite> 

的{CACHE_URL}和{C:1}位,以保持使用的協議。例如。 HTTP請求www.websitea.com/websiteb/foo.html的用戶通過HTTP「重寫」到websiteb.com/foo.html,並且對HTTPS上的websitea.com/websiteb/bar.html的請求被「重寫」到HTTPS上的websiteb.com/bar.html。

現在,對於某些網站B頁面,我們希望用戶僅使用HTTPS - 這是在我們的SomePageViewModel的ShouldBeHttps屬性中設置的。所以下面的代碼是在ActionFilterAttribute使用:

public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     if (filterContext.HttpContext.Request.IsSecureConnection) 
      return; 

     var result = filterContext.Result as ViewResult; 
     if (result != null) 
     { 
      if ((result.Model as SomePageViewModel).ShouldBeHttps) 
      { 
       HandleNonHttpsRequest(filterContext); 
      } 
     } 
    } 

    protected virtual void HandleNonHttpsRequest(ActionExecutedContext filterContext) 
    { 
     if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) 
      throw new InvalidOperationException("The method must be a GET"); 
     string url = filterContext.HttpContext.Request.Url.ToString().Replace("http:", "https:"); 
     filterContext.Result = new RedirectResult(url); 
    } 

比方說www.websitea.com/websiteb/securepage.html會導致在ShouldBeHttps屬性真值。

現在,當我通過HTTP轉到websiteb.com/securepage.html,直接在服務器上測試它時,我會在HTTPS上正確重定向(狀態碼302)到websiteb.com/securepage.html。

我希望當我訪問www.websitea.com/websiteb/securepage.html上的HTTP時,我將被重定向到www.websitea.com/websiteb/securepage.html上的HTTPS。但是,我的瀏覽器最終處於重定向循環(ERR_TOO_MANY_REDIRECTS)。我可以在小提琴手看到的TextView標籤,它似乎是正確設置:

<html> 
<head> 
    <title>Object moved</title> 
</head> 
<body> 
<h2>Object moved to <a href="https://www.websitea.com/websiteb/securepage.html">here</a>.</h2> 
</body> 
</html> 

但頭選項卡顯示:

Response Headers 
HTTP/1.1 302 Found 
(...) 
Transport 
    Location: http://www.websitea.com/websiteb/securepage.html 

因此而不是去到https,這又是http和再次擊中過濾器,依此類推。

有什麼我失蹤?它是一些IIS設置?

+0

我覺得問題是重寫規則沒有看到差異b在傳入的http或https呼叫之間,因此它將重寫爲https。 – Quintium

+0

我相信它可以正常工作,我可以看到HTTPS的請求www.websitea.com/websiteb/index.html正確地轉到HTTPS websiteb.com/index.html和HTTP到HTTP; HttpContext.Current.Request上的IsSecureConnection屬性正確設置爲true/false。 – patrykgliwinski

+0

啊。我懂了。我誤讀了那部分。但是如果你想讓網站a在重寫規則中重定向到網站b,應該不是而是? – Quintium

回答

-1

試試這個配置。作品爲我的網站

<rule name="Redirect to HTTPS" stopProcessing="true"> 
<match url="(.*)" /> 
<conditions><add input="{HTTPS}" pattern="^OFF$" /> 
</conditions> 
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" /> 
</rule> 
+0

不,不需要,我需要使用ShouldBeHttps值,這是「計算」服務器端只有在重寫完成後;所以我不能「硬編碼「在重寫操作中使用https。 – patrykgliwinski

-1

這主要是在IIS

錯誤1.停用URL重寫緩存

2.增加 「不安全」 變量您的規則

3。清除緩存

而且最好的辦法,如果你可以在代碼中做到這一點,而無需使用重寫

根據什麼使用IIS的版本你,這能解決你的bug:

x64

x86

KB2974666