RequireHttps 在带参数的重定向上失败

发布于 2024-08-09 23:19:03 字数 2126 浏览 4 评论 0原文

除了简单地从 HTTP 重定向到 HTTPS 之外, RequireHttps 属性还将我的 URL 的问号 ? 更改为 %3F。毫不奇怪,这会破坏事情。

为什么?我该如何修复它?

场景:

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

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

    ; _
    公共类消息控制器
        继承System.Web.Mvc.Controller
    
        函数 Compose() 作为 ActionResult
            ...
            如果……那么
                抛出新异常(Net.HttpStatusCode.Unauthorized.ToString)
            结束如果
            ...
            返回视图()
        结束功能
    
        ...
    
    结束课程
    
  3. 该操作引发异常。

  4. 异常由我的 Error.aspx 页面处理

    <%@ 页面语言="VB" Inherits="System.Web.Mvc.ViewPage(Of System.Web.Mvc.HandleErrorInfo)" %>
    
    <脚本运行=“服务器”>
        Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
            如果 Model.Exception.Message = Net.HttpStatusCode.Unauthorized.ToString 那么
                response.redirect("/signin?ReturnUrl=" + Url.Encode(request.Path))
            结束如果
            ...
        结束子
    
    
    ...
    
  5. 我被重定向到我的登录页面 http://www.example.com/signin?ReturnUrl=%2fmessage %2fcompose

  6. 但是我我正在使用 HTTP 而不是 HTTPS。我的操作需要 HTTPS。 (RemoteRequireHttps继承自RequireHttps 并覆盖其 OnAuthorization 方法以简单地忽略本地主机。)

    ; _
    公共类AccountController
        继承System.Web.Mvc.Controller
    
         _
        函数 SignIn() 作为 ActionResult
            ...
        结束功能
    
        ...
    
    结束课程
    
  7. 我被重定向到 https://www.example.com/signin%3FReturnUrl=%2fmessage%2fcompose

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

错误的请求

RequireHttps 似乎将我的问号 ? 更改为 %3F

Besides simply redirecting from HTTP to HTTPS, the RequireHttps attribute also changes my URL's question mark ? to %3F. Unsurprisingly, that breaks things.

Why? How do I fix it?

scenario:

  1. I try to navigate to http://www.example.com/message/compose

  2. But let's say I don't have permission to view that page.

    <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. The action throws an exception.

  4. The exception is handled by my Error.aspx page

    <%@ 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>
    
    ...
    
  5. I'm redirected to my Sign In page at http://www.example.com/signin?ReturnUrl=%2fmessage%2fcompose

  6. But I'm using HTTP and not HTTPS. My action requires HTTPS. (That RemoteRequireHttps inherits from RequireHttps and overrides its OnAuthorization method to simply disregard localhost.)

    <HandleError()> _
    Public Class AccountController
        Inherits System.Web.Mvc.Controller
    
        <RemoteRequireHttps()> _
        Function SignIn() As ActionResult
            ...
        End Function
    
        ...
    
    End Class
    
  7. I'm redirected to https://www.example.com/signin%3FReturnUrl=%2fmessage%2fcompose

  8. I see this error in my browser:

Bad Request

It seems that RequireHttps changes my question mark ? to %3F.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-08-16 23:19:03

我们正在考虑使用 ISAPI Rewrite 这样的 URL 重写器来在此类事件到达您的代码之前对其进行处理。当然,这只有在您有权访问服务器的情况下才有效。

另外,如果您使用 IIS 7,我相信它带有内置的 URL 重写功能。

We're considering using a URL rewriter like ISAPI Rewrite to handle things like this before they ever get to your code. Of course, this would only work if you have access to the server.

Also, if you're using IIS 7, I believe it comes with URL rewriting functionality built in.

调妓 2024-08-16 23:19:03

我的对我自己的问题的回答 这里可能会有所帮助(似乎是 MVC 2 Preview 2 中的错误)

我可以问一下为什么你不使用 [Authorize] 或 自定义授权属性。如果您使用 .NET 身份验证,则应使用 [Authorize]。

注意:即使您确实使用[授权],它实际上并不能解决您遇到的问题。

My answer to my own question here might help (appears to be a bug in MVC 2 Preview 2)

Can I ask why you're not using [Authorize] or a custom authorization attribute. If you're using .NET authentication you should use [Authorize].

Note: even if you do use [Authorize] it doesnt actually fix the problem you're experiencing.

深海蓝天 2024-08-16 23:19:03

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

场景:

我尝试导航到 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

但我使用的是 HTTP 而不是 HTTPS。我的操作需要 HTTPS。

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

没有问号。没问题。

I've just reengineered this to avoid the question mark (and also exception throws) altogether.

scenario:

I try to navigate to http://www.example.com/message/compose

But let's say I don't have permission to view that page.

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

I'm redirected to my Sign In page. My route looks like this:

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

So that's at address http://www.example.com/signin/message/compose

But I'm using HTTP and not HTTPS. My action requires HTTPS.

Public Class AccountController
    Inherits System.Web.Mvc.Controller

    <RemoteRequireHttps()> _
    Function SignIn(ByVal ReturnUrl As String) As ActionResult
        ...
    End Function

    ...

End Class

I'm redirected to https://www.example.com/signin/message/compose

No question mark. No problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文