1

Suppose I have a textbox on my ASP.net web page where a user enters some text.

On another page, I have the following: <p><%=userText%></p>, where userText refers to the text that the user entered on the previous page.

What do I need to do to make the text display properly if it contains special characters? Also, users can enter text that will be displayed to other users, so it is imperative that I prevent abuse (such as injecting <script> tags.

1
  • sanitizing user input is one of the easiest tasks to do, as long as you don't need anything special. As soon as you want to allow the user to add elements to the page, the problem becomes significantly harder. Commented Jul 15, 2011 at 21:11

1 Answer 1

4

You should HTML encode the data, see here.

String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.