【问题标题】:Switching Between HTTP and HTTPS in ASP.NET MVC 1.0在 ASP.NET MVC 1.0 中的 HTTP 和 HTTPS 之间切换
【发布时间】:2010-03-10 03:31:21
【问题描述】:

我正在使用 MVC 1.0 并创建了一个“RequireSSLAttribute”(类似于 ASP.NET MVC 1.0 Futures 中的那个,但忽略了本地计算机的 SSL 指令)。我想启用 SSL 注册和登录页面以保护正在发送的密码。但是,我希望网站的其余部分不是 SSL。

通过将 [RequireSSL] 属性添加到我的控制器的注册和登录方法,我能够成功地让应用程序使用 HTTPS 重定向到相应的页面。但是,注册或登录后的所有页面都继续使用 SSL。

有没有什么方法可以让应用切换回 HTTP 而不必创建我必须添加到所有其他控制器方法的“RequireNonSslAttribute”?

谢谢。

【问题讨论】:

  • 我遇到了同样的问题,我通过创建与您所做的相同的事情来解决它。我很想知道是否有人也可以在这里提供帮助。
  • 你可能还想看看这个:owasp.org/index.php/…

标签: asp.net-mvc ssl


【解决方案1】:

如果您只是将其添加到您在登录后重定向到的控制器操作中会怎样?或者在您的基本控制器中添加重定向。例如,我们在基础 OnActionExecuting 中做这样的事情:

        if (req.IsSecureConnection && 
            filterContext.RouteData.Values["controller"]
                         .ToString().ToLower() != "home")
        {
            string url = req.Url.ToString().ToLower().Replace("http:", "https:");
            res.Redirect(url);
        }

这是我们完成基本相同事情的最快方式(我们的家庭控制器具有登录类型的操作)。

【讨论】:

  • 这应该可以。我现在唯一的问题是 BeginForm 呈现的表单标记中的 action 属性创建了一个不是 HTTPS 的相对 URL。看起来我必须使用 BeginForm 方法的重载版本使用 HTTPS 绝对 URL 覆盖 action 属性。应该可以,但是啊。
【解决方案2】:

在您的 ControllerBase 类中,您可以覆盖 Controller.OnAuthorization,然后检查是否在 Controller Action 上设置了 RequireHttps 属性(RequireHttpsAttribute 是 MVC2 中的新属性)。如果未设置 RequireHttps 属性并且请求在 SSL 下,则将重定向结果返回到非 ssl url。这样您就不必同时设置控制器属性,然后将 controller.action 名称放在其他位置。

protected override void OnAuthorization(AuthorizationContext filterContext)
{
    Boolean requireHttps = false;
    requireHttps = filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), false).Count() >= 1;

    //the RequireHttpsAttribute set on the Controller Action 
    //will handle redirecting to Https.
    // We just need to handle any requests 
    //that are already under SSL but should not be.

    //If this request is under ssl but yet the controller action 
    // does not require it, then redirect to the http version.
    if (Request.IsSecureConnection && !requireHttps)
    {
        UriBuilder uriBuilder = new UriBuilder(Request.Url);

        //change the scheme
        uriBuilder.Scheme = "http";
        uriBuilder.Port = 80;

        filterContext.Result = this.Redirect(uriBuilder.Uri.AbsoluteUri);
    }


    base.OnAuthorization(filterContext);
}

【讨论】:

    【解决方案3】:

    在用户仍然登录的情况下从 HTTPS 迁移到 HTTP 会带来安全风险,因为在随后的帖子中,身份验证 cookie 仍会从浏览器传输给用户,但这次不会被加密。

    一旦登录,最好(从安全角度)继续使用 HTTPS。在任何情况下,只要 TCP 会话处于初始握手状态,就不会对性能造成太大影响。

    【讨论】:

      【解决方案4】:

      我在 Controller.OnAturhotization 上使用了这段代码。

      var redirect = string.Format(
       "https://{0}{1}", 
       filterContext.HttpContext.Request.Url.Authority,
       Response.ApplyAppPathModifier(filterContext.HttpContext.Request.Url.AbsolutePath));
      var queryString = filterContext.HttpContext.Request.QueryString.ToString();
      if (!string.IsNullOrEmpty(queryString))
       redirect += "?" + queryString;
      
      filterContext.Result = new RedirectResult(redirect);
      

      Response.ApplyAppPathModifier 支持无cookie会话。

      HttpResponse.ApplyAppPathModifier Method (System.Web)

      【讨论】:

        猜你喜欢
        • 2011-10-20
        • 1970-01-01
        • 2014-01-11
        • 1970-01-01
        • 2012-08-17
        • 2010-11-09
        • 2012-05-14
        • 1970-01-01
        • 2015-02-18
        相关资源
        最近更新 更多