尝试将HTTP重定向到HTTPS(C#,IIS),但HTTPS在响应头中转回HTTP - 重定向循环

pat*_*ski 9 c# iis https redirect url-rewrite-module

我在同一个IIS服务器上设置了两个IIS网站.网站A包含大部分内容,这是用户在请求我们的地址时显示的内容(例如www.websitea.com).网站B是一个单独的项目,只包含整个内容的一部分,因此它是内部的(在IIS中绑定到websiteb.com),但通过URL重写,用户可以通过键入www.websitea.com/websiteb来访问它.

URL重写在网站A的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>
Run Code Online (Sandbox Code Playgroud)

{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);
    }
Run Code Online (Sandbox Code Playgroud)

假设www.websitea.com/websiteb/securepage.html将在ShouldBeHttps属性中生成一个真值.

现在,当我通过访问HTTP上的websiteb.com/securepage.html直接在服务器上测试它时,我在HTTPS上正确地重定向(状态代码302)到websiteb.com/securepage.html.

我希望当我在HTTP上访问www.websitea.com/websiteb/securepage.html时,我将通过HTTPS重定向到www.websitea.com/websiteb/securepage.html.但是,我的浏览器最终会出现重定向循环(ERR_TOO_MANY_REDIRECTS).我可以在Fiddler的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>
Run Code Online (Sandbox Code Playgroud)

但是标题选项卡显示:

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

因此,它不再是https,而是http,再次点击过滤器,依此类推.

有什么我想念的吗?它是一些IIS设置?

Mic*_*dze -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>
Run Code Online (Sandbox Code Playgroud)