HttpAntiForgeryException 被视为 403 和 500,具体取决于是否启用自定义错误
最近,我们在 ASP.Net MVC 3 中开发的网站上实现了 CSRF 保护。
使用一些混杂的技术,我们有了一个可行的解决方案。
但是,我们的应用程序错误处理部分无法正常工作,因为根据自定义错误是否打开,为异常设置的状态代码有所不同。
当自定义错误打开时,我们会收到 403,关闭时会设置 500 状态代码。
抛出的异常是 HttpAntiForgeryException,根据我对 MVC 源代码的检查,它应该是 500。
它被抛出到 ActionFilterAttribute 中,这是否将异常包装在 403 中?
IIS 是否做了一些奇怪的事情?
任何想法将不胜感激。
干杯,
We have recently implemented CSRF protection on a web site we are working on in ASP.Net MVC 3.
Using a bit of a mish mash of techniques, we have a working solution.
However our error handling part of the app is not working properly as the status code set for the exception is different depending on whether custom errors is turned on or not.
When custom errors is on, we get a 403, when off a 500 status code being set.
The exception being throw is an HttpAntiForgeryException, which should be a 500 from my inspection of the MVC source.
It's being thrown inside a ActionFilterAttribute, does this wrap the exception inside a 403?
Is IIS doing something odd?
Any thoughts would be appreciated.
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经实现了 MVC 3 反 CSRF 解决方案,并且在故意使检查失败时(通过在获取之后和发布之前抑制客户端上的 cookie),我从未收到 403 代码。
检查具有 ValidateAntiForgeryToken 属性的操作是否已完成。 cutomErrors 设置为 RemoteOnly,然后设置为 On,然后设置为 Off。测试了两次,第一次在 Cassini 开发服务器上,第二次在 IIS6 上。
我总是收到 500 响应代码。
我猜问题出在您正在使用的 customErrors 处理中。您还应该检查是否有任何 httpModule 对错误事件执行一些自定义逻辑。 (如果您使用 MVC HandleError 属性,则不太可能,因为此 MVC customError 处理会在 HttpModule 处理之前捕获错误,然后永远不会看到错误。)如果
您的问题中没有关于您使用哪种 customError 处理机制的更多详细信息,则很难给出更多指示。
我的既不是 MVC 标准(使用 HandleError 作为控制器属性、操作属性或全局过滤器),也不是经典的 ASP.NET 标准。它也不是 ELMAH。
由于“历史”原因,我处理 Application_Error 事件中的错误(并且因为我发现支持 ISS 6 和 7 错误处理更容易,我需要这样做,直到我们的服务器迁移完成)。 Application_Error 事件中我的错误处理逻辑的代码如下:
I have the MVC 3 anti-CSRF solution implemented, and I never receive 403 code when purposely making the check to fail (by suppressing cookies on client side after the get and before the post).
Check done on an action having a ValidateAntiForgeryToken attribute. cutomErrors set with RemoteOnly, then On, then Off. Tested twice, first time on Cassini dev server, second time on IIS6.
I have always received a 500 response code.
I guess the trouble is located in the customErrors handling you are using. You should also check if you have any httpModule doing some custom logic on error events. (Unlikely if you use MVC HandleError attribute, as this MVC customError handling traps the errors before HttpModule handling, which then never see errors.)
Without more details in your question about which customError handling mechanism are you using, it is difficult to give more directions.
Mine is neither the MVC standard one (using HandleError as controller attribute, action attribute or global filter), nor the classical asp.net one. It is not ELMAH either.
I handle errors in Application_Error event due to "historical" reasons (and for I found it easier to support both ISS 6 and 7 error handling, which I need to do till the migration of our servers is done). Code of my error handling logic in Application_Error event is like :