Redirect http to https in default.aspx

我怕爱的太早我们不能终老 提交于 2019-12-13 00:42:54

问题


our site address is like "http://members.XXX.XX", so I want to redirect to the "https://members.XXX.XX" no matter user type "members.XXX.XX" or "http://members.XXX.XX", here is my code, and i put it in the default.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsSecureConnection == false)
        {
            Response.Redirect(Request.Url.ToString().Replace("http://", "https://"));
        }

        if (!Request.Url.ToString().StartsWith("http://") || !Request.Url.ToString().StartsWith("https://"))
            Response.Redirect("https://" + Request.Url.ToString());

        Response.Redirect("~/pages/login.aspx");
    }

which doesn't work, anyone can help? many thanks

Edit: We didn't do that in IIS because we instored the ELMAH which is a error logging system. My coworker said if we did that the ELMAH wouldn't work


回答1:


Instead of doing it in code simply let IIS handle it for you.

IIS7
Require ssl via IIS and capture the error code 403.4 to do the redirect for you. Step by step guide "IIS7 Redirect HTTP to HTTPS"

IIS6
You can create a redirect site that runs on port 80 to handle your redirect to https. Read more about it "How to Force Redirection From HTTP to HTTPS on IIS 6.0".




回答2:


You can add to Global.asax

    protected void Application_BeginRequest()
    {

        if (!Context.Request.Url.AbsoluteUri.Contains("localhost") && !Context.Request.IsSecureConnection)
            Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
    }


来源:https://stackoverflow.com/questions/11231668/redirect-http-to-https-in-default-aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!