Share via

Solution need to allow "<" and ">" characters in asp.net textbox fields without compromising security

Pughazendhi Chandrakasan 1 Reputation point
2022-10-25T13:43:40.77+00:00

There is a requirement for an ASP.NET application. A textbox should allow "<" (example: <test@Arjun .com>) characters.

I am getting "A potentially dangerous Request.Form value was detected by the client" error when user enter with < character.

In the @Anonymous directive of the aspx page, I tried ValidateRequest=false. It worked. Nevertheless, turning off security isn't a good idea.

As an example, let's assume that I am looking for the most secure way to allow the < character without compromising security, like script injection attacks.

It would be helpful if you could advise

Thanks in advance.

Developer technologies | ASP.NET | Other

2 answers

Sort by: Most helpful
  1. AgaveJoe 31,341 Reputation points
    2022-10-26T13:22:18.14+00:00

    What is the simplest way to implement the same functionality in an asp.net Webform without too much modification?

    Simple is in the eye of the developer.

    One option is writing a JavaScript/jQuery script to URI encode the text inputs before the inputs are submitted. Decode the inputs on the server. You still need to be careful of the input values so you don't end up with a cross site script vulnerability.

    $('form').submit(function(e) {  
        $('.SpecialChars').each(function (index, element) {  
            element.value = encodeURI(element.value);  
        });  
    });  
    

    I used a css class to identify the input to encode.

    <asp:TextBox ID="SpecialChars" runat="server" CssClass="SpecialChars"></asp:TextBox>  
    

    The code behind

    Literal1.Text = Server.UrlDecode(SpecialChars.Text);  
    

    I'm not sure if encodeURI will handle ever possible situation. Another option is following the recommendations in the error. Disable request validation and write custom validation which sorta' like the inverse of the the option above. In either case some form of validation logic is needed.

    Request Validation in ASP.NET

    0 comments No comments

  2. Michael Taylor 61,221 Reputation points
    2022-10-25T14:40:03.853+00:00

    Yes, do not turn off request validation on the page level if at all possible. Unfortunately there isn't an easy way around this.

    One option is to go find the old Ajax Control Toolkit. It had an HTML editor control that you could use instead of Textbox. Alternatively hook into the submission process on the client side, HTML encode the contents of the textbox before it gets sent to the server. On the server side use Server.HtmlDecode to convert it back to HTML.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.