好的,所以我想在我的 ASP.NET MVC 网站的某些部分上强制使用 https/ssl。在这里找到了很多很棒的资源,包括 ActionFilter:http: //blog.tylergarlick.com/index.php/2009/07/mvc-redirect-to-http-and-https/
每当我启用 ActionFilter 但是我最终都会进入重定向循环。问题是如果我在地址栏中输入https://www.mysite.com/,request.url总是等于http://www.mysite.com/。
操作过滤器的代码如下,据我所知,我不会在标准设置之外进行任何 url 重写、重定向、自定义路由或修改 url。是否有任何常见/不常见的原因导致这种情况发生和/或任何解决方法或修复?该站点目前托管在 NetworkSolutions - 是否有可能与 IIS 配置有关?任何帮助将不胜感激。
public class RedirectToHttps : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Helpers just so I don’t have to type so much
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
// Make sure we’re in https or local
if (!request.IsSecureConnection && !request.IsLocal)
{
string redirectUrl = request.Url.ToString().Replace("http:", "https:");
response.Redirect(redirectUrl);
}
base.OnActionExecuting(filterContext);
}
}