代码之家  ›  专栏  ›  技术社区  ›  Zack Peterson

RequireHttps在带有参数的重定向时失败

  •  0
  • Zack Peterson  · 技术社区  · 15 年前

    除了简单地从HTTP重定向到HTTPS之外 RequireHttps ? %3F . 不出所料,这会破坏一切。

    为什么?我该怎么修?

    脚本:

    1. 我试着导航到 http://www.example.com/message/compose

    2. 但是假设我没有查看该页面的权限。

      <HandleError()> _
      Public Class MessageController
          Inherits System.Web.Mvc.Controller
      
          Function Compose() As ActionResult
              ...
              If ... Then
                  Throw New Exception(Net.HttpStatusCode.Unauthorized.ToString)
              End If
              ...
              Return View()
          End Function
      
          ...
      
      End Class
      
    3. 异常由我的错误.aspx页码

      <%@ Page Language="VB" Inherits="System.Web.Mvc.ViewPage(Of System.Web.Mvc.HandleErrorInfo)" %>
      
      <script runat="server">
          Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
              If Model.Exception.Message = Net.HttpStatusCode.Unauthorized.ToString Then
                  response.redirect("/signin?ReturnUrl=" + Url.Encode(request.Path))
              End If
              ...
          End Sub
      </script>
      
      ...
      
    4. 我被重定向到我的登录页面 http://www.example.com/signin?ReturnUrl=%2fmessage%2fcompose

    5. 但我用的是HTTP而不是HTTPS。我的操作需要HTTPS。(那个 RemoteRequireHttps 继承自 所需TTP 并重写其OnAuthorization方法以忽略localhost。)

      <HandleError()> _
      Public Class AccountController
          Inherits System.Web.Mvc.Controller
      
          <RemoteRequireHttps()> _
          Function SignIn() As ActionResult
              ...
          End Function
      
          ...
      
      End Class
      
    6. 我被重定向到 https://www.example.com/signin%3FReturnUrl=%2fmessage%2fcompose

    7. 我在浏览器中看到此错误:

    错误的请求

    看来RequireHttps改变了我的问号 ? %3楼

    3 回复  |  直到 7 年前
        1
  •  1
  •   Shea Daniels    15 年前

    我们正在考虑使用一个URL重写器 ISAPI Rewrite

    另外,如果您使用的是iis7,我相信它内置了URL重写功能。

        2
  •  1
  •   Community CDub    7 年前

    我的 answer to my own question 这里可能会有帮助(在mvc2预览版2中出现了一个bug)

    我能问一下你为什么不使用[授权]或 custom authorization attribute . 如果使用.NET身份验证,则应使用[授权]。

    注意:即使你使用[Authorize],它也不能真正解决你遇到的问题。

        3
  •  0
  •   Zack Peterson    15 年前

    我只是重新设计了它,以避免问号(以及异常抛出)。

    脚本:

    我试着导航到 http://www.example.com/message/compose

    但是假设我没有查看该页面的权限。

    Public Class MessageController
        Inherits System.Web.Mvc.Controller
    
        Function Compose() As ActionResult
            ...
            If ... Then
                Return RedirectToAction("SignIn", "Account", New With {.ReturnUrl = Request.Path})
            End If
            ...
            Return View()
        End Function
    
        ...
    
    End Class
    

    我被重定向到我的登录页面。我的路线是这样的:

    routes.MapRoute( _
        "SignIn", _
        "signin/{*ReturnUrl}", _
        New With {.controller = "Account", _
                  .action = "SignIn", _
                  .ReturnUrl = ""} _
    )
    

    那是地址 http://www.example.com/signin/message/compose

    Public Class AccountController
        Inherits System.Web.Mvc.Controller
    
        <RemoteRequireHttps()> _
        Function SignIn(ByVal ReturnUrl As String) As ActionResult
            ...
        End Function
    
        ...
    
    End Class
    

    我被重定向到 https://www.example.com/signin/message/compose