2008-12-03 148 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.

相關問題