我在同一个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设置?
我觉得问题是重写规则没有看到差异b在传入的http或https呼叫之间,因此它将重写为https。 – Quintium
我相信它可以正常工作,我可以看到HTTPS的请求www.websitea.com/websiteb/index.html正确地转到HTTPS websiteb.com/index.html和HTTP到HTTP; HttpContext.Current.Request上的IsSecureConnection属性正确设置为true/false。 – patrykgliwinski
啊。我懂了。我误读了那部分。但是如果你想让网站a在重写规则中重定向到网站b,应该不是 是 而是? –
Quintium