【发布时间】:2017-06-19 18:58:13
【问题描述】:
我这几天一直在搜索这个问题,做了很多研究,但我就是想不通。
我正在尝试控制我的页面以将它们重定向到 http 或 https,无论我需要与否。
我创建了一个属性放在我需要在 HTTPS 中运行的每个页面上方
[AttributeUsage(AttributeTargets.Class)]
public sealed class RequireSSL : Attribute { }
然后我运行一个 HttpModule 来检查请求的每个页面。
public class DartsGhentPipeline : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += Context_PreRequestHandlerExecute;
}
private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Request;
HttpResponse response = application.Response;
Page page = application.Context.CurrentHandler as Page;
if (page == null)
return;
bool requireSSL = page.GetType().GetCustomAttributes(typeof(RequireSSL), true).Length > 0;
bool isSecureConnection = request.IsSecureConnection;
string url = string.Format("{0}://www.dartsghent.be{1}", requireSSL ? Uri.UriSchemeHttps : Uri.UriSchemeHttp, HttpContext.Current.Request.RawUrl);
if (requireSSL != isSecureConnection)
response.RedirectPermanent(url);
}
public void Dispose() { }
}
这里的问题是,当我重定向到相同的网址或任何其他网址时。浏览器拒绝从 http 切换到 https 或其他导致重定向循环的方式,浏览器将阻止这种情况发生。
我发现当我将网址替换为 www 时,我的网站会完美运行。或没有 www。但我不能在我有 test.dartsghent.be 的子网站上使用它
谁能告诉我如何解决这个问题,请不要使用 urlrewriter 模块。
感谢任何帮助
注意:我没有使用 MVC
如您所见,页面不断地自行重定向。我正在使用 RedirectPermanent(url) 到 https,但即使重定向被触发,它也不会移动到 https,所以它一直在尝试。
【问题讨论】:
-
“浏览器拒绝切换”是什么意思?您是否看到响应中设置了正确的重定向标头(使用您的浏览器开发工具)?究竟是什么意外的浏览器行为?
-
在 Chrome 中我得到了以下行为 dartsghent.be --> wwww.dartsghent.be = 成功 dartsghent.be --> dartsghent.be = 页面将保留在 http 协议上
-
我测试的越多,我就越认为问题出在重定向上。即使我清楚地告诉我的重定向转到 https 链接。看起来重定向正在将我的重定向本身移动到与当前相同的协议,只要域相同。有什么解决方案吗?
-
您是否使用 SSL 卸载或任何类型的负载平衡器?
标签: c# asp.net https response.redirect ihttpmodule