我需要能够允许包含'<'等字符的查询字符串和'>'。但是,将id = mi< ke之类的内容放入URL将输出错误页面:
从客户端检测到一个潜在危险的Request.QueryString值(id =“mi< ke”)。
如果我首先对url进行url编码(创建id = mi%3Cke),我仍会得到同样的错误。我可以通过将ValidateRequest =“false”放入Page指令来解决这个问题,但如果可能的话,我宁愿不这样做。
那么无论如何在查询字符串中允许这些字符并且不关闭ValidateRequest?
编辑:我想让用户也可以手动输入网址,因此以某种方式对其进行编码可能无效。
答案 0 :(得分:5)
我遇到了类似的问题。我选择base64编码查询字符串来解决它。 使用
System.Text.ASCIIEncoding.ASCII.GetBytes
将字符串作为字节 然后
System.Convert.ToBase64String
将其变成“安全”字符串。
要取回它,请使用:
System.Convert.FromBase64String
然后:
System.Text.ASCIIEncoding.ASCII.GetString
扭转流动的极性。
答案 1 :(得分:1)
有点谷歌搜索,我不这么认为。 在代码甚至运行之前似乎发生异常,因此您无法捕获异常。 我喜欢编码为base64或者其他想法。
答案 2 :(得分:0)
您可以加密您的ID值,而不是URL编码来解决问题。然后,您可能需要对加密的字符串进行URL编码。
答案 3 :(得分:0)
我认为你有一些选择。您可以在指示并关闭ValidateRequest时执行此操作。然后,您需要自己处理任何输入清理。或者您可以只允许某些字符,并让用户使用元语言输入它们,即代替'<'使用'['并替换'>'使用']'或在将自己提交给元语言(或Base64)之前重新编码这些。自己重新编码需要Javascript可用于使用禁用字符的查询。您可能仍需要进行输入清理。
快速查看jquery实现:
$(document).ready( function() {
$('form').bind('submit', function() {
$('form' > 'input[type=text]').each( function(i) {
if (this.value) {
this.value = encode(this.value);
}
});
});
});
function encode(value) {
return ...suitable encoding...
}
答案 4 :(得分:0)
我正在处理同样的问题,但我偶然发现了这种javascript编码方法:
<script type="text/javascript">
var unencodedText = "This is my text that contains whitespaces and characters like and Ø";
var encodedText = "";
var decodedText = "";
alert('unencodedText: ' + unencodedText);
//To encode whitespaces and the 'Ø' character - use encodeURI
encodedText = encodeURI(unencodedText);
//We see that whitespaces and 'Ø' are encoded, but the '' is still there:
alert('encodedText: ' + encodedText);
//If we decode it we should get our unencodedText back
decodedText = decodeURI(encodedText);
alert('decodedText: ' + decodedText);
//To also encode the '' we use the encodeURIComponent
encodedText = encodeURIComponent(unencodedText);
//Now all the characters have been encoded:
alert('encodedText: ' + encodedText);
//To get our unencodedText back we now need to use the decodeURIComponent
decodedText = decodeURIComponent(encodedText);
alert('decodedText: ' + decodedText);
</script>
如果您正在处理更复杂的符号,那么您可能希望将encodeURIComponent用于网址。
窃取了这个宝石