我想重定向到强制https:
的网页例如
Response.Redirect("~/Login.aspx");
我想重定向到
https://myserver/Login.aspx
我怎么强迫https?
答案 0 :(得分:5)
感谢Silky让我开始。
我最终得到了
var url = String.Format("https://{0}{1}",
Request.ServerVariables["HTTP_HOST"] ,
ResolveUrl("~/Login.aspx"));
Response.Redirect(url);
答案 1 :(得分:0)
我喜欢在我想强制https的任何页面中从Page_Load调用此方法。具体而言,从用户可以输入密码的任何页面,例如登录,注册和密码重置页面。
protected void ForceHTTPS()
{
string serverName = Request.ServerVariables["SERVER_NAME"];
string pagePath = Page.AppRelativeVirtualPath.Replace("~", "");
string queryString = Request.Url.Query;
if (serverName != "localhost" && !Request.IsSecureConnection)
{
Response.Redirect("https://" + SECURE_DOMAIN + pagePath + queryString);
}
}
我为SECURE_DOMAIN使用预定义常量而不是仅仅从Request.ServerVariables [" SERVER_NAME"]中读取它的原因是我们的SSL证书仅在存在" WWW&#34。在域名的开头,所以我想确保强制使用该域名,以防用户使用没有www。的域名浏览网站,或者可能使用其他域名别名。
如果服务器名称为" localhost"我不进行重定向。这样,从Visual Studio运行时,代码也可以在我的开发环境中运行。