问题
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