2008-12-03 149 views
3

我需要能够允许包含字符'<'和'>'的查询字符串。然而,把类似ID = MI <科进入该网址会输出一个错误页面,指出:允许危险的查询字符串

从客户端(ID =“MI <科”)检测到有潜在危险的Request.QueryString值。

如果我第一次url编码url(创建id = mi%3CKE)我仍然得到相同的错误。我可以通过在页面指令中加入ValidateRequest =“false”来解决这个问题,但如果可能的话,我宁愿不这样做。

无论如何,允许这些字符在查询字符串中,而不是关闭ValidateRequest?

编辑:我想让用户能够手动输入网址,因此以某种方式对它们进行编码可能无法正常工作。

+0

这块宝石是不是一个畸形的URL,如果它包含“<' or '>”?它们不应该分别编码为%3C和%3E吗? – rmeador 2008-12-03 23:31:52

+0

是的,我提出了我也试过这个问题。仍然没有骰子。 – 2008-12-03 23:47:38

回答

5

我碰到类似这样的问题。我选择base64编码查询字符串来解决它。 使用

System.Text.ASCIIEncoding.ASCII.GetBytes 

得到的字符串作为字节 然后

System.Convert.ToBase64String 

把它变成一个 “安全” 的字符串。

把它找回来,使用:

System.Convert.FromBase64String 

然后:

System.Text.ASCIIEncoding.ASCII.GetString 

反向流动的极性。

0

除了URL编码,您可以加密您的ID值以解决问题。您可能需要对加密的字符串进行URL编码。

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... 
} 
1

有点谷歌上搜索,我不这么认为。 在您的代码运行之前似乎发生了异常,因此您无法捕获异常。 我喜欢编码为base64或其他想法。

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的网址。

而且我偷this link.

相关问题