Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用https c#强制页面,出现错误此网页有重定向循环_C#_Http_Redirect_Https - Fatal编程技术网

使用https c#强制页面,出现错误此网页有重定向循环

使用https c#强制页面,出现错误此网页有重定向循环,c#,http,redirect,https,C#,Http,Redirect,Https,我一直在尝试做http重定向到https的两天,任何帮助将不胜感激!我在共享托管环境中托管我的网站,不允许在IIS中更改任何内容 这是c#登录页面中的代码 protected void Page_Load(object sender, EventArgs e) { if (Request.Url.ToString().IndexOf("http://") != -1) { string redirectUrl = Request.Url.ToString().Re

我一直在尝试做http重定向到https的两天,任何帮助将不胜感激!我在共享托管环境中托管我的网站,不允许在IIS中更改任何内容

这是c#登录页面中的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Url.ToString().IndexOf("http://") != -1)
    {
        string redirectUrl = Request.Url.ToString().Replace("http://", "https://");
        //Response.Write(redirectUrl);
        Response.Redirect(redirectUrl);
    }      
}

我得到的错误是“这个网页有一个重定向循环”。我还尝试在web.config文件中执行重定向,但出现了相同的错误。是否有强制页面使用https的方法?

这样做可以直接询问您是否使用https

if(Request.IsSecureConnection)
我使用此选项重定向到我在HTTPS中的登录:

if(!IsPostBack){
    if(!Request.IsSecureConnection)
      Response.Redirect("https://" & Request.Url.Host & "/Login.aspx")

......
......
....
...
}

如果您支持负载平衡器类型的服务,我相信您需要确保
X-Forwarded-For
标头已通过。hi asawyer,有没有示例说明我如何通过X-Forwarded-For标头?您不能触摸IIS,但主机环境如何。它是负载平衡器吗?你没有透露细节。问题是浏览器使用HTTPS与负载平衡服务器对话,但负载平衡器在HTTP端口80上与IIS内部对话。您可能完全可以,但应用程序无法知道或执行这一点。解决方案是将balance软件配置为使用
x-forwarded-proto
(not-for,sorry)头传递原始请求的http/https值,并在应用程序中使用
request.Headers
集合检查该值。抱歉,asawyer,我在123 reg共享主机上托管了网站,我唯一能控制的就是使用ftp上传文件。我是否可以在c#或web.config文件中执行此操作?非常感谢您发送电子邮件,请他们开始使用
x-forwarded-proto
,否则我不知道该告诉您什么。找一个更好的主机?嘿,路易斯,我刚刚尝试了你的代码,但仍然给了我同样的错误。这个网页有一个重定向循环。这对我来说很好,它只在连接不安全的情况下重定向,我不明白你为什么会出现这个错误。我知道,这没有意义,它让我发疯。该页面将重定向到https/:XXXX/login.aspx,并且仍然显示“此网页具有重定向循环”。不过还是要感谢您的帮助。@user205526也许这会帮助您消除困惑。