c# - 如何允许 "potentially dangerous"参数值?

标签 c# asp.net-web-api

我有一个 Web API 项目。我的端点之一允许进行字符串搜索,其中可能包含特殊字符。

[RoutePrefix("api/Search")]
[ValidateInput(false)] // this is *supposed* to allow us to search using "unsafe" characters, like %, & etc.
public class SearchController : ApiController
{
    ...
    [HttpGet]
    [Route("{searchValue}", Name = "GenericSearch")]
    public async Task<IHttpActionResult> Search(string searchValue)
    {
        ...
    }
}

当我调用 api/Search/fred 时,它按预期工作。

当我调用 api/Search/fred%25 时,出现此错误:

[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (%).] System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +561 System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +54

尽管我的 Controller 装饰有 [ValidateInput(false)],并且根据我在其他地方找到的其他答案,我将 requestValidationMode 添加到我的 Web .配置:

<system.web>
  ...
  <httpRuntime requestValidationMode="2.0" />
</system.web>

我还需要翻转什么 secret 开关?

最佳答案

您缺少 httpRuntime 上允许无效字符的属性。

<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />

关于c# - 如何允许 "potentially dangerous"参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43869012/

相关文章:

c# - 在 Entity Framework 中设置

c# - EF6 重试过程为 SqlQuery 命令抛出 "The SqlParameter is already contained by another SqlParameterCollection"

c# - 窗体 Visual C# 中的控制台

c# - httpclient 调用 webapi 以发布数据不起作用

c# - 如何返回createatroute位置?

java - 在浏览器中显示 PDF

c# - Audit.NET Entity Framework Core - 相关实体管理

c# - 如何解决 SqlNullValueException?

asp.net-web-api - ASP.net 5 中的 IApplicationBuilder.UseJwtBearerAuthentication 扩展在哪里?

c# - AngularJS Web Api AntiForgeryToken CSRF