Directly writing user input (for example, an HTTP request parameter) to a webpage, without properly sanitizing the input first, allows for a cross-site scripting vulnerability.

To guard against cross-site scripting, consider using a library that provides suitable encoding functionality, such as the System.Net.WebUtility class, to sanitize the untrusted input before writing it to the page. For other possible solutions, see the references.

The following example shows the page parameter being written directly to the server error page, leaving the website vulnerable to cross-site scripting.

Sanitizing the user-controlled data using the WebUtility.HtmlEncode method prevents the vulnerability:

  • OWASP: XSS (Cross Site Scripting) Prevention Cheat Sheet.
  • Wikipedia: Cross-site scripting.