How to redirect HTTP to HTTPS in MVC application (IIS7.5)

前端 未结 11 2119
情歌与酒
情歌与酒 2020-11-29 21:39

I need to redirect my HTTP site to HTTPS, have added below rule but I am getting 403 Error when tried using http://www.example.com, it works fine when I type https://www.exa

11条回答
  •  盖世英雄少女心
    2020-11-29 22:19

    I did it thusly, since a local debug session uses custom port numbers:

        protected void Application_BeginRequest()
        {
            if (!Context.Request.IsSecureConnection)
            {
                if (HttpContext.Current.Request.IsLocal)
                {
                    Response.Redirect(Context.Request.Url.ToString().Replace("http://localhost:25885/", "https://localhost:44300/"));
                }
                else
                {
                    Response.Redirect(Context.Request.Url.ToString().Replace("http://", "https://"));
                }
            }
        }
    

    Preferably there would be some way to get the URL and SSL URL programmatically...

提交回复
热议问题