首页 > 解决方案 > 从客户端将数据和文件发布到 Web api 中检测到潜在危险的 Request.Form 值

问题描述

我有一个表单,用于将数据和文件发布到web api endpoint. 数据字段之一是RichTextField使用summernote插件。

发布到的angularjs脚本endpoint如下所示:

$scope.transaction = {};
var files = $scope.myFile;
var fd = new FormData();

angular.forEach(files, function (value, key) {
    fd.append("file" + key, value[0]);
});

fd.append("fileContent", JSON.stringify({ fileContent: $scope.transaction }));

dataService.uploadFile(baseAddress + "/Generate", fd)
.success(function (data) {}).error(function (data) {  });

接受请求的端点如下所示:

  [HttpPost]
    public async Task<IHttpActionResult> Generate()
    {
        try
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

           var contentResult = System.Web.HttpContext.Current.Request.Form;                
           var key = contentResult.AllKeys[0];
           var val = contentResult.GetValues(key)[0];

此时var val = contentResult.GetValues(key)[0];我收到此错误消息:

A potentially dangerous Request.Form value was detected from the client

我试过这个:

[System.Web.Http.HttpPost, System.Web.Mvc.ValidateInput(false)]

<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />

标签: c#angularjsasp.net-web-api

解决方案


您必须允许 html 标签,因为 Summernote 数据包含它们。为此,您需要将AllowHtmlAttribute添加到您的方法中。

你可能需要做一些修改,因为它可以很容易地做 xss 和其他事情。


推荐阅读