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

前端 未结 30 2145
刺人心
刺人心 2020-11-21 05:24

Every time a user posts something containing < or > in a page in my web application, I get this exception thrown.

I don\'t want to go

30条回答
  •  不思量自难忘°
    2020-11-21 05:35

    If you don't want to disable ValidateRequest you need to implement a JavaScript function in order to avoid the exception. It is not the best option, but it works.

    function AlphanumericValidation(evt)
    {
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
            ((evt.which) ? evt.which : 0));
    
        // User type Enter key
        if (charCode == 13)
        {
            // Do something, set controls focus or do anything
            return false;
        }
    
        // User can not type non alphanumeric characters
        if ( (charCode <  48)                     ||
             (charCode > 122)                     ||
             ((charCode > 57) && (charCode < 65)) ||
             ((charCode > 90) && (charCode < 97))
           )
        {
            // Show a message or do something
            return false;
        }
    }
    

    Then in code behind, on the PageLoad event, add the attribute to your control with the next code:

    Me.TextBox1.Attributes.Add("OnKeyPress", "return AlphanumericValidation(event);")
    

提交回复
热议问题