2010-10-07 11 views
35

Sto effettuando una chiamata ajax utilizzando jQuery a una pagina ASP.NET che funge da pagina del server ajax a salvare i dati che sto inviando ad esso nella stringa di query. Nella pagina ASP.NET quando sto cercando di leggere la querystring sto ottenendo questo errore:Un valore Request.QueryString potenzialmente pericoloso è stato rilevato dal client durante l'invio di markup html da jquery post call alla pagina asp.net

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

Ho impostato il ValidateRequest="false" nella mia pagina. Non voglio impostarlo per tutte le pagine. Così ha fatto in livello di pagina invece che a livello di configurazione:

var content = "<h3>Sample header</h3><p>sample para</p>" 
    content = encodeURIComponent(content); 
    var url = "../Lib/ajaxhandler.aspx?mode=savecontent&page=home&ltxt=" + content; 

    $.post(url, function (data) { 
     //check return value and do something 
    }); 

e nella mia pagina asp.net:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ajaxhandler.aspx.cs" ValidateRequest="false" Inherits="MyProject.Lib.ajaxhandler" %> 

Ma quando mando testo normale al posto del codice HTML, funziona benissimo.

+0

Sei tu completamente sicuri che hai messo ValidateRequest = "false" sulla giusta pagina? –

+0

Oh Sì. Ho ricontrollato. – Shyju

+0

Possibile duplicato di [ValidateRequest = "false" non funziona in Asp.Net 4] (http://stackoverflow.com/questions/2673850/validaterequest-false-doesnt-work-in-asp-net-4) – nothingisnecessary

risposta

34

Se questo è ASP.NET 4, c'era un breaking change con ValidateRequest. Vedere this StackOverflow question per ulteriori informazioni su requestValidationMode.

+0

Oh Sì. Questo problema è venuto quando ho aggiornato la mia versione di framework a 4.0.it ha funzionato con 2.0 ieri. – Shyju

-1

set ValidateRequest = "false" nella parte superiore della pagina asp.

8

C'è già una buona risposta per questo, e qui fornirò le informazioni in modo da non dover fare clic sui collegamenti.

Quando si esegue ASP.NET 4.0, è necessario impostare quanto segue nel file web.config RequestValidationMode="2.0".

A cosa serve questa proprietà?

A value that indicates which ASP.NET version-specific approach to validation will be used. The default is 4.0.

Quindi quali sono i valori possibili?

  • 4.0 (the default). The HttpRequest object internally sets a flag that indicates that request validation should be triggered whenever any
    HTTP request data is accessed. This guarantees that the request
    validation is triggered before data such as cookies and URLs are
    accessed during the request. The request validation settings of the
    pages element (if any) in the configuration file or of the @ Page
    directive in an individual page are ignored.

  • 2.0. Request validation is enabled only for pages, not for all HTTP requests. In addition, the request validation settings of the pages element (if any) in the configuration file or of the @ Page directive in an individual page are used to determine which page requests to validate.

Information citated from this msdn site.

1

Se si desidera aggiungere una logica di convalida personalizzato per una determinata pagina ASP.NET o per uno o più parametri di stringa di query senza impostare ValidateRequest="false" per l'intera pagina - la seguente " soluzione "hacky" potrebbe essere utile:

public partial class MyPage : System.Web.UI.Page 
{ 
    private string SomeUnvalidatedValue { get; set; } 

    public override void ProcessRequest(HttpContext context) 
    { 
     var queryString = context.Request.QueryString; 

     var readOnly = queryString.GetType().GetProperty("IsReadOnly", 
      System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 

     readOnly.SetValue(queryString, false); 

     var unvalidatedValue = context.Request.Unvalidated.QueryString["SomeKey"]; 
     // for RequestValidationMode="2.0" 
     //var unvalidatedValue = context.Request.QueryString["SomeKey"]; 

     // custom logic goes here 

     // you could store unvalidated value here and then remove it from the query string 
     SomeUnvalidatedValue = unvalidatedValue; 
     queryString["SomeKey"] = string.Empty; 
     // or just remove all "potentially dangerous" symbols, for example 
     if (!string.IsNullOrEmpty(unvalidatedValue)) 
     { 
      queryString["SomeKey"] = Regex.Replace(unvalidatedValue, 
       "(\\<+[a-z!/\\?])|(&\\#)", new MatchEvaluator((m) => 
       { 
        return m.Value.Replace("<", string.Empty).Replace("&#", string.Empty); 
       }), RegexOptions.IgnoreCase); 
     } 

     readOnly.SetValue(queryString, true); 

     // keep other request validation logic as is 
     base.ProcessRequest(context); 
    } 
} 

L'espressione regolare eseguita come risultato dell'analisi del metodo ASP.NET: CrossSiteScriptingValidation.IsDangerousString

Codice testato con .NET 4.5.2, modalità integrata IIS, con e senza RequestValidationMode="2.0".

0

ho creato un paio di metodi riutilizzabili basati su VAV's answer

public static string ExtractUnvalidatedValue(HttpRequest request, string key) 
     { 
      var unvalidatedValue = HttpUtility.UrlDecode(request.Unvalidated.QueryString[key]); 
      // for RequestValidationMode="2.0" 
      //var unvalidatedValue = context.Request.QueryString["SomeKey"]; 

      // remove all "potentially dangerous" symbols 
      return ReplacePotentiallyDangerousSymbols(unvalidatedValue, string.Empty); 
     } 

    public static string ReplacePotentiallyDangerousSymbols(string unvalidatedValue, string valueToReplace="") 
     { 
      if (!string.IsNullOrEmpty(unvalidatedValue)) 
      { 
       //The regular expression made as result of this ASP.NET method analyzing: CrossSiteScriptingValidation.IsDangerousString http://referencesource.microsoft.com/#System.Web/CrossSiteScriptingValidation.cs,3c599cea73c5293b 
       unvalidatedValue = Regex.Replace(unvalidatedValue, 
        "(\\<+[a-z!/\\?])|(&\\#)", 
        new MatchEvaluator((m) => { return m.Value.Replace("<", valueToReplace).Replace("&#", valueToReplace); }), RegexOptions.IgnoreCase); 
      } 
      return unvalidatedValue; 
     } 
Problemi correlati