【发布时间】:2013-07-09 11:54:59
【问题描述】:
我正在使用 Microsoft ASP.NET Web API 构建 RESTful 服务。
我的问题涉及 Web API 在出现问题时(例如 400 Bad Request 或 404 Not Found)返回给用户的 HttpErrors。
问题是,我不想在响应内容中获得序列化的 HttpError,因为它有时会提供太多信息,因此它违反了 OWASP 安全规则,例如:
请求:
http://localhost/Service/api/something/555555555555555555555555555555555555555555555555555555555555555555555
作为回应,我当然得到 400,但包含以下内容信息:
{
"$id": "1",
"Message": "The request is invalid.",
"MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'MyNamespaceAndMethodHere(Int32)' in 'Service.Controllers.MyController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
这样的东西不仅表明我的 WebService 是基于 ASP.NET WebAPI 技术(这还不错),而且它还提供了一些关于我的命名空间、方法名称、参数等的信息。
我尝试在 Global.asax 中设置 IncludeErrorDetailPolicy
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;
是的,效果不错,现在结果不包含 MessageDetail 部分,但我仍然不想得到这个 HttpError。
我还构建了我的自定义 DelegatingHandler,但它也会影响我自己在控制器中生成的 400 和 404,这是我不希望发生的。
我的问题是: 有什么方便的方法可以从响应内容中删除序列化的 HttpError 吗?我希望用户得到他的错误请求的只是响应代码。
【问题讨论】:
标签: asp.net asp.net-web-api response http-error