代码之家  ›  专栏  ›  技术社区  ›  Justin C

ASP.NET:重定向到https的最佳实践

  •  8
  • Justin C  · 技术社区  · 16 年前

    我的问题是,IIS 6或7上的ASP.NET站点将http页面重定向到https的最佳实践是什么?是否有最佳实践,或者所有备选方案是否相同?

    4 回复  |  直到 16 年前
        1
  •  12
  •   Pavel Chuchuva grapeot    16 年前

    我会使用URL重写来实现这一点。为什么?因为它易于实现,不需要修改应用程序,并且易于维护。

    在IIS7上,您可以使用 URL rewrite module

    <!-- http:// to https:// rule -->
    <rule name="ForceHttpsBilling" stopProcessing="true">
      <match url="(.*)billing/(.*)" ignoreCase="true" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="false" />
      </conditions>
      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
    </rule>
    

    在IIS6上,您必须使用第三方库。我使用IIRF( http://www.codeplex.com/IIRF

        2
  •  3
  •   Clarence Klopfstein    16 年前

    1. 在HTTPModule中。HttpModules是在处理任何请求之前运行的,因此您可以在那里执行URL检查和重定向。这就是我要做的。
    2. 在Global.asax中。

    我不会把代码放在每一页,那只是糟糕的编程。

        3
  •  1
  •   Community Mohan Dere    9 年前

    我会调用响应。在页面加载中重定向。它比生成javascript更简单,并且将向客户端发送更少的字节。

    Code example

        4
  •  1
  •   Andy    16 年前

    我使用以下操作属性将流量转换为一个或另一个:

    public class ForceConnectionSchemeAttribute : ActionFilterAttribute
    {
        private string scheme;
    
        public ForceConnectionSchemeAttribute(string scheme)
        {
            this.scheme = scheme.ToLower();
        }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Uri url = filterContext.HttpContext.Request.Url;
            if (url.Scheme != scheme)
            {
                string secureUrl = String.Format("{0}://{1}{2}", scheme, url.Host, url.PathAndQuery);
                filterContext.Result = new RedirectResult(secureUrl);
            }
        }
    }
    
    
    // Suppose I always want users to use HTTPS to access their personal info:
    [ForceConnectionScheme("https")]
    public class UserController: Controller
    {
        // blah
    }