繁体   English   中英

从客户端检测到潜在危险的Request.Form值,请编码帮助

[英]A potentially dangerous Request.Form value was detected from the client, encoding help please

所以我试图将一个字符串作为参数通过Js中的Post提交给asp.net服务,但是我遇到了一些困难。 在声明之前,我无权访问服务器,也无法触摸验证,我严格从外部客户端进行访问。 我得到这个回应

System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (message="...t;img src='http://192.168.1...").
    at System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection)
    at System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, RequestValidationSource requestCollection)
    at System.Web.HttpRequest.get_Form()
    at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
    at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

我发送的消息是:

xcvxzcvzxcvxcvzxcv< br /><img src='http://192.168.1.1:82/UserUploads/Images/65968/20130122020024996.jpg' alt='User Image' />

我使用以下代码进行编码:

htmlEncode: function(str) {
        str = str.replace(/&/g, '&amp;');
        str = str.replace(/'/g, '&#39;');
        str = str.replace(/"/g, "&quot;");
        str = str.replace(/</g, '&lt;');
        str = str.replace(/>/g, '&gt;');
        return str;
    },

产生:

xcvxzcvzxcvxcvzxcv&lt; br /&gt;&lt;img src=&#39;http://192.168.1.1:82/UserUploads/Images/65968/20130122020802027.jpg&#39; alt=&#39;User Image&#39; /&gt;

我已经通过了多个验证器,并检查了我的编码,但无法确定是什么导致了问题。 我唯一的猜测是http://引起了JavaScript错误中所示的问题,但我不确定。 任何帮助或见识将不胜感激。

问题是'的编码。 根据user409762,&#的组合在asp.net中被标记为危险。

所以现在我的编码看起来像这样,并且工作正常。

htmlEncode: function(str) {
    str = str.replace(/&/g, '&amp;');
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/</g, '&lt;');
    str = str.replace(/>/g, '&gt;');
    return str;
},

使用Jquery,您可以像该link一样执行编码和解码。

function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM