<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bricex</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Bricex"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Bricex"/>
		<updated>2026-04-09T15:44:38Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=185279</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=185279"/>
				<updated>2014-11-12T14:32:10Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in the key/value pairs of a URL query string.&lt;br /&gt;
&amp;lt;pre&amp;gt;var url = &amp;quot;https://www.bing.com/search?q=&amp;quot; + HttpUtility.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==AntiXssEncoder==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
===CssEncode===&lt;br /&gt;
Encodes the specified string for use in cascading style sheets (CSS). This method encodes all characters except those that are in the safe list, by using the CSS escape character (/) followed by up to six hexadecimal digits.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert\000028\000027XSS\000020Attack\000021\000027\000029\00003B&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;user@contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;user\000040contoso\00002Ecom&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===HtmlFormUrlEncode===&lt;br /&gt;
Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;. This method encodes all characters except those that are in the safe list. Characters are encoded by using %SINGLE_BYTE_HEX notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert%28%27XSS+Attack%21%27%29%3B&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;user@contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;user%40contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===XmlAttributeEncode===&lt;br /&gt;
Encodes the specified string for use in XML attributes, and is slightly more restrictive than XmlEncode below. This method encodes all characters except those that are in the safe list. Characters are encoded by using &amp;amp;#DECIMAL; notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#32;Attack!&amp;amp;amp;apos;);&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert('XSSあAttack!');&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;amp;amp;lt;script&amp;amp;amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#12354;Attack!&amp;amp;amp;apos;);&amp;amp;amp;lt;/script&amp;amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===XmlEncode===&lt;br /&gt;
Encodes the specified string for use in XML. This method encodes all characters except those that are in the safe list. Characters are encoded by using &amp;amp;#DECIMAL; notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert(&amp;amp;amp;#39;XSS&amp;amp;amp;#32;Attack!&amp;amp;amp;#39;);;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert('XSSあAttack!');&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;amp;amp;lt;script&amp;amp;amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#12354;Attack!&amp;amp;amp;apos;);&amp;amp;amp;lt;/script&amp;amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://blogs.msdn.com/b/sfaust/archive/2008/09/02/which-asp-net-controls-automatically-encodes.aspx Which ASP.NET Controls Automatically Encode?]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391 HTML Encoded Data-Binding Expressions]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184993</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184993"/>
				<updated>2014-11-08T18:39:37Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Including example inputs/outputs&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in the key/value pairs of a URL query string.&lt;br /&gt;
&amp;lt;pre&amp;gt;var url = &amp;quot;https://www.bing.com/search?q=&amp;quot; + HttpUtility.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==AntiXssEncoder==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
===CssEncode===&lt;br /&gt;
Encodes the specified string for use in cascading style sheets (CSS). This method encodes all characters except those that are in the safe list, by using the CSS escape character (/) followed by up to six hexadecimal digits.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert\000028\000027XSS\000020Attack\000021\000027\000029\00003B&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;user@contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;user\000040contoso\00002Ecom&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===HtmlFormUrlEncode===&lt;br /&gt;
Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;. This method encodes all characters except those that are in the safe list. Characters are encoded by using %SINGLE_BYTE_HEX notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert%28%27XSS+Attack%21%27%29%3B&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;user@contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;user%40contoso.com&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===XmlAttributeEncode===&lt;br /&gt;
Encodes the specified string for use in XML attributes, and is slightly more restrictive than XmlEncode below. This method encodes all characters except those that are in the safe list. Characters are encoded by using &amp;amp;#DECIMAL; notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#32;Attack!&amp;amp;amp;apos;);&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert('XSSあAttack!');&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;amp;amp;lt;script&amp;amp;amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#12354;Attack!&amp;amp;amp;apos;);&amp;amp;amp;lt;/script&amp;amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===XmlEncode===&lt;br /&gt;
Encodes the specified string for use in XML. This method encodes all characters except those that are in the safe list. Characters are encoded by using &amp;amp;#DECIMAL; notation.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert('XSS Attack!');&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;alert(&amp;amp;amp;#39;XSS&amp;amp;amp;#32;Attack!&amp;amp;amp;#39;);;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert('XSSあAttack!');&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;&amp;amp;amp;lt;script&amp;amp;amp;gt;alert(&amp;amp;amp;apos;XSS&amp;amp;amp;#12354;Attack!&amp;amp;amp;apos;);&amp;amp;amp;lt;/script&amp;amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391 HTML Encoded Data-Binding Expressions]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184627</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184627"/>
				<updated>2014-11-03T21:16:45Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Don't Rely on Request Validation for XSS Protection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer provided in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input requires validating each field of user supplied data. This should start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls] and/or [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes] to check for:&lt;br /&gt;
* Required fields&lt;br /&gt;
* Correct data type and length&lt;br /&gt;
* Data falls within an acceptable range&lt;br /&gt;
* Whitelist of allowed characters&lt;br /&gt;
&lt;br /&gt;
Any string input that is returned to the client should be encoded using an appropriate method, such as those provided via [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is enabled by reviewing the following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|style=&amp;quot;width: 20%;&amp;quot;|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation is set to true (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;true&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is set to true (or not set at all) at the page level: &amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly requestValidationMode should be set to &amp;quot;4.0&amp;quot; (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.5+&lt;br /&gt;
|There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that requestValidationMode has been set to &amp;quot;4.5&amp;quot; in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web API&lt;br /&gt;
|ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Extending Request Validation==&lt;br /&gt;
If you are using ASP.NET 4.0 or higher, you have the option of extending or replacing the Request Validation logic by providing your own class that descends from &amp;lt;code&amp;gt;System.Web.Util.RequestValidator&amp;lt;/code&amp;gt;. By implementing this class, you can determine when validation occurs and what type of request data to perform validation on.&lt;br /&gt;
&amp;lt;pre&amp;gt;public class CustomRequestValidation : RequestValidator&lt;br /&gt;
{&lt;br /&gt;
    protected override bool IsValidRequestString(&lt;br /&gt;
        HttpContext context, &lt;br /&gt;
        string value, &lt;br /&gt;
        RequestValidationSource requestValidationSource, &lt;br /&gt;
        string collectionKey, &lt;br /&gt;
        out int validationFailureIndex)&lt;br /&gt;
    {&lt;br /&gt;
        validationFailureIndex = -1;&lt;br /&gt;
&lt;br /&gt;
        // This is just an example and should not&lt;br /&gt;
        // be used for production code.&lt;br /&gt;
        if (value.Contains(&amp;quot;&amp;lt;%&amp;quot;)) &lt;br /&gt;
        {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
        else // Leave any further checks to ASP.NET. &lt;br /&gt;
        {&lt;br /&gt;
            return base.IsValidRequestString(&lt;br /&gt;
                context, &lt;br /&gt;
                value, &lt;br /&gt;
                requestValidationSource, &lt;br /&gt;
                collectionKey, &lt;br /&gt;
                out validationFailureIndex);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This class is then registered in web.config using &amp;lt;code&amp;gt;requestValidationType&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;httpRuntime requestValidationType=&amp;quot;CustomRequestValidation&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.util.requestvalidator(v=vs.110).aspx RequestValidator Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184624</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184624"/>
				<updated>2014-11-03T21:05:50Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Encoding Output Values in Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in the key/value pairs of a URL query string.&lt;br /&gt;
&amp;lt;pre&amp;gt;var url = &amp;quot;https://www.bing.com/search?q=&amp;quot; + HttpUtility.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391 HTML Encoded Data-Binding Expressions]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184622</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184622"/>
				<updated>2014-11-03T21:03:46Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Encoding Output Values in Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in the key/value pairs of a URL query string.&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391 HTML Encoded Data-Binding Expressions]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184614</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184614"/>
				<updated>2014-11-03T20:55:02Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097391 HTML Encoded Data-Binding Expressions]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184612</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184612"/>
				<updated>2014-11-03T20:53:31Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Adding HTML encoding of data binding expressions in ASP.NET 4.5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
Starting with ASP.NET 4.0 you can HTML encode values in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can also HTML encode the result of data-binding expressions. Just add a colon (:) to the end of the &amp;lt;%# prefix that marks the data-binding expression:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;asp:TemplateField HeaderText=&amp;quot;Name&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;ItemTemplate&amp;gt;&amp;lt;%#: Item.Products.Name %&amp;gt;&amp;lt;/ItemTemplate&amp;gt;&lt;br /&gt;
&amp;lt;/asp:TemplateField&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184576</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184576"/>
				<updated>2014-11-03T17:34:51Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Encoding Output Values in HTML markup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184575</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184575"/>
				<updated>2014-11-03T17:34:17Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
*[[ASP.NET Request Validation]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184568</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184568"/>
				<updated>2014-11-03T14:56:42Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Adding - Extending Request Validation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer provided in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input requires validating each field of user supplied data. This should start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls] and/or [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes] to check for:&lt;br /&gt;
* Required fields&lt;br /&gt;
* Correct data type and length&lt;br /&gt;
* Data falls within an acceptable range&lt;br /&gt;
* Whitelist of allowed characters&lt;br /&gt;
&lt;br /&gt;
Any string input that is stored, returned to the client, or interpreted should be encoded using an appropriate method, such as those provided via [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is enabled by reviewing the following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|style=&amp;quot;width: 20%;&amp;quot;|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation is set to true (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;true&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is set to true (or not set at all) at the page level: &amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly requestValidationMode should be set to &amp;quot;4.0&amp;quot; (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.5+&lt;br /&gt;
|There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that requestValidationMode has been set to &amp;quot;4.5&amp;quot; in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web API&lt;br /&gt;
|ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Extending Request Validation==&lt;br /&gt;
If you are using ASP.NET 4.0 or higher, you have the option of extending or replacing the Request Validation logic by providing your own class that descends from &amp;lt;code&amp;gt;System.Web.Util.RequestValidator&amp;lt;/code&amp;gt;. By implementing this class, you can determine when validation occurs and what type of request data to perform validation on.&lt;br /&gt;
&amp;lt;pre&amp;gt;public class CustomRequestValidation : RequestValidator&lt;br /&gt;
{&lt;br /&gt;
    protected override bool IsValidRequestString(&lt;br /&gt;
        HttpContext context, &lt;br /&gt;
        string value, &lt;br /&gt;
        RequestValidationSource requestValidationSource, &lt;br /&gt;
        string collectionKey, &lt;br /&gt;
        out int validationFailureIndex)&lt;br /&gt;
    {&lt;br /&gt;
        validationFailureIndex = -1;&lt;br /&gt;
&lt;br /&gt;
        // This is just an example and should not&lt;br /&gt;
        // be used for production code.&lt;br /&gt;
        if (value.Contains(&amp;quot;&amp;lt;%&amp;quot;)) &lt;br /&gt;
        {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
        else // Leave any further checks to ASP.NET. &lt;br /&gt;
        {&lt;br /&gt;
            return base.IsValidRequestString(&lt;br /&gt;
                context, &lt;br /&gt;
                value, &lt;br /&gt;
                requestValidationSource, &lt;br /&gt;
                collectionKey, &lt;br /&gt;
                out validationFailureIndex);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This class is then registered in web.config using &amp;lt;code&amp;gt;requestValidationType&amp;lt;/code&amp;gt;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;system.web&amp;gt;&lt;br /&gt;
    &amp;lt;httpRuntime requestValidationType=&amp;quot;CustomRequestValidation&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/system.web&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.util.requestvalidator(v=vs.110).aspx RequestValidator Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184567</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184567"/>
				<updated>2014-11-03T14:37:12Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Selectively Disabling Request Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer provided in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input requires validating each field of user supplied data. This should start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls] and/or [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes] to check for:&lt;br /&gt;
* Required fields&lt;br /&gt;
* Correct data type and length&lt;br /&gt;
* Data falls within an acceptable range&lt;br /&gt;
* Whitelist of allowed characters&lt;br /&gt;
&lt;br /&gt;
Any string input that is stored, returned to the client, or interpreted should be encoded using an appropriate method, such as those provided via [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is enabled by reviewing the following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|style=&amp;quot;width: 20%;&amp;quot;|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation is set to true (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;true&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is set to true (or not set at all) at the page level: &amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly requestValidationMode should be set to &amp;quot;4.0&amp;quot; (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.5+&lt;br /&gt;
|There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that requestValidationMode has been set to &amp;quot;4.5&amp;quot; in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web API&lt;br /&gt;
|ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184566</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184566"/>
				<updated>2014-11-03T14:36:09Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Enabling Request Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer provided in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input requires validating each field of user supplied data. This should start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls] and/or [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes] to check for:&lt;br /&gt;
* Required fields&lt;br /&gt;
* Correct data type and length&lt;br /&gt;
* Data falls within an acceptable range&lt;br /&gt;
* Whitelist of allowed characters&lt;br /&gt;
&lt;br /&gt;
Any string input that is stored, returned to the client, or interpreted should be encoded using an appropriate method, such as those provided via [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is enabled by reviewing the following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|style=&amp;quot;width: 20%;&amp;quot;|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation is set to true (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;true&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is set to true (or not set at all) at the page level: &amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly requestValidationMode should be set to &amp;quot;4.0&amp;quot; (or not set at all) in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.5+&lt;br /&gt;
|There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that requestValidationMode has been set to &amp;quot;4.5&amp;quot; in web.config: &amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web API&lt;br /&gt;
|ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible and ensure you have implemented field level input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184565</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184565"/>
				<updated>2014-11-03T14:25:42Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Don't Rely on Request Validation for XSS Protection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer provided in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input requires validating each field of user supplied data. This should start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls] and/or [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes] to check for:&lt;br /&gt;
* Required fields&lt;br /&gt;
* Correct data type and length&lt;br /&gt;
* Data falls within an acceptable range&lt;br /&gt;
* Whitelist of allowed characters&lt;br /&gt;
&lt;br /&gt;
Any string input that is stored, returned to the client, or interpreted should be encoded using an appropriate method, such as those provided via [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled by reviewing he following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation has not been disabled in web.config: &amp;lt;code&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is not disabled at the page level: &amp;lt;code&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that it has '''NOT''' been set to 2.0: &amp;lt;code&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===ASP.NET 4.5+=== &lt;br /&gt;
There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has been set to &amp;quot;4.5&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible and ensure you have implemented field level input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184564</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184564"/>
				<updated>2014-11-03T14:17:32Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Changing sub title&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==IHtmlString==&lt;br /&gt;
If you have model properties that are used to display raw HTML you should consider using the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface and will instruct ASP.NET to skip output encoding when using &amp;lt;code&amp;gt;&amp;lt;%: model.Property %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@model.Property&amp;lt;/code&amp;gt; in HTML markup. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184561</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184561"/>
				<updated>2014-11-03T00:16:21Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
==Don't Rely on Request Validation for XSS Protection==&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''NOT''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer supported in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
Fully protecting your application from malicious input involves validating each field of user supplied data. For Web Forms based applications this will start with [http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx ASP.NET Validation Controls]. ASP.NET MVC and Web API based applications should decorate model classes with [http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx DataAnnotations attributes]. Any string input that is stored, returned to the client, or interpreted should be encoded using an appropriate method, such as those from [[ASP.NET Output Encoding#Enhanced_Encoding|AntiXssEncoder]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled by reviewing he following areas:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|ASP.NET Web Forms (Global)&lt;br /&gt;
|Ensure that request validation has not been disabled in web.config: &amp;lt;code&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET Web Forms (Page Level)&lt;br /&gt;
|Check to make sure request validation is not disabled at the page level: &amp;lt;code&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|ASP.NET 4.0+&lt;br /&gt;
|Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that it has '''NOT''' been set to 2.0: &amp;lt;code&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===ASP.NET 4.5+=== &lt;br /&gt;
There are enhancements added to request validation starting with ASP.NET 4.5 that include deferred (&amp;quot;lazy&amp;quot;) validation, the ability to opt-out at the server control level, and the ability to access unvalidated data. In order to leverage these enhancements you will need to ensure that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has been set to &amp;quot;4.5&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;4.5&amp;quot; targetFramework=&amp;quot;4.5&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible and ensure you have implemented field level input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications prior to v4.5, you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.5 you can disable request validation at the individual server control level by setting &amp;lt;code&amp;gt;ValidateRequestMode&amp;lt;/code&amp;gt; to &amp;quot;Disabled&amp;quot;.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;asp:TextBox ID=&amp;quot;txtASPNet&amp;quot; ValidateRequestMode=&amp;quot;Disabled&amp;quot; runat=&amp;quot;server&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx Validation ASP.NET Controls]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx How to: Validate Model Data Using DataAnnotations Attributes]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.asp.net/aspnet/overview/aspnet-and-visual-studio-2012/whats-new#_Toc318097379 New ASP.NET Request Validation Features]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ui.control.validaterequestmode(v=vs.110).aspx Control.ValidateRequestMode Property]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184560</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184560"/>
				<updated>2014-11-02T22:15:50Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. This check adds protection from markup or code in the URL query string, cookies, or posted form values that might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack. Request validation helps to prevent this kind of attack by throwing a &amp;quot;potentially dangerous value was detected&amp;quot; error and halting page processing if it detects input that may be malicious, such as markup or code in the request. &lt;br /&gt;
&lt;br /&gt;
===Don't Rely on Request Validation for XSS Protection===&lt;br /&gt;
Request validation is generally desirable and should be left enabled for [[defense in depth]]. It should '''not''' be used as your sole method of XSS protection, and does not guarantee to catch every type of invalid input. There are known, documented bypasses (such as JSON requests) that will not be addressed in future releases, and the request validation feature is no longer supported in ASP.NET vNext.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=User:Bricex&amp;diff=184552</id>
		<title>User:Bricex</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=User:Bricex&amp;diff=184552"/>
				<updated>2014-11-02T13:21:59Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:BriceWilliams.jpg|left|200px]] Brice Williams is an application security consultant with a focus on improving the state of information security within the software development life cycle. Brice brings over 17 years experience in the software security space, with a focus on the Microsoft technology stack. He is a member of the OWASP NYC chapter and a contributing author to the OWASP .NET Project.&lt;br /&gt;
&lt;br /&gt;
Brice can be reached at brice.williams [at] owasp.org and on Twitter at [https://twitter.com/bricex @bricex].&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=File:BriceWilliams.jpg&amp;diff=184549</id>
		<title>File:BriceWilliams.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=File:BriceWilliams.jpg&amp;diff=184549"/>
				<updated>2014-11-02T13:15:32Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=User:Bricex&amp;diff=184548</id>
		<title>User:Bricex</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=User:Bricex&amp;diff=184548"/>
				<updated>2014-11-02T13:09:43Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Brice Williams is an application security consultant with a focus on improving the state of information security within the software development life cycle. Brice brings over 17 years experience in the software security space, with a focus on the Microsoft technology stack. He is a member of the OWASP NYC chapter and a contributing author to the OWASP .NET Project.&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184421</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184421"/>
				<updated>2014-10-30T19:25:30Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Preventing Double Encoding==&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@untrustedData&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184419</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184419"/>
				<updated>2014-10-30T19:23:12Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Validating User Input */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184418</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184418"/>
				<updated>2014-10-30T19:22:54Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Encoding Output Values in Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Preventing Double Encoding==&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@untrustedData&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184417</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184417"/>
				<updated>2014-10-30T19:20:33Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Preventing Double Encoding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode untrusted data in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlToken = Server.UrlTokenEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Preventing Double Encoding==&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@untrustedData&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184416</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184416"/>
				<updated>2014-10-30T19:20:10Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Preventing Double Encoding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode untrusted data in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlToken = Server.UrlTokenEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Preventing Double Encoding==&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: untrustedData %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@untrustedData &amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184415</id>
		<title>ASP.NET Output Encoding</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Output_Encoding&amp;diff=184415"/>
				<updated>2014-10-30T19:19:23Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Initial page content&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
Cross-site scripting attacks exploit vulnerabilities in web page validation by injecting client-side script code. The script code embeds itself in response data, which is sent back to an unsuspecting user. In addition to validating input, any data retrieved from untrusted or shared sources should be encoded on output. For example: data retrieved from a database that may have had malicious input persisted to it.&lt;br /&gt;
&lt;br /&gt;
==Validating Input==&lt;br /&gt;
See the [[ASP.NET Request Validation]] article for details on how request validation can be used to protect against malicious input.&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in Code==&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtml = Server.HtmlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode untrusted data for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrl = Server.UrlEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode untrusted data in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlToken = Server.UrlTokenEncode(untrustedData);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Encoding Output Values in HTML markup==&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: untrustedData%&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@untrustedData&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Preventing Double Encoding==&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@userInput&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enhanced Encoding==&lt;br /&gt;
By default the ASP.NET encoding methods use a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to the common &amp;lt;code&amp;gt;HtmlEncode&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UrlEncode&amp;lt;/code&amp;gt; methods, the Anti-Cross Site Scripting Library provides the following &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; methods for more specialized output encoding needs:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|CssEncode&lt;br /&gt;
|Encodes the specified string for use in cascading style sheets (CSS).&lt;br /&gt;
|-&lt;br /&gt;
|HeaderNameValueEncode&lt;br /&gt;
|Encodes a header name and value into a string that can be used as an HTTP header.&lt;br /&gt;
|-&lt;br /&gt;
|HtmlAttributeEncode&lt;br /&gt;
|Encodes and outputs the specified string for use in an HTML attribute. &lt;br /&gt;
|-&lt;br /&gt;
|HtmlFormUrlEncode&lt;br /&gt;
|Encodes the specified string for use in form submissions whose MIME type is &amp;quot;application/x-www-form-urlencoded&amp;quot;.&lt;br /&gt;
|-&lt;br /&gt;
|JavaScriptStringEncode&lt;br /&gt;
|Encodes a string for use in JavaScript.&lt;br /&gt;
|-&lt;br /&gt;
|UrlPathEncode&lt;br /&gt;
|Encodes path strings for use in a URL.&lt;br /&gt;
|-&lt;br /&gt;
|XmlAttributeEncode&lt;br /&gt;
|Encodes the specified string for use in XML attributes.&lt;br /&gt;
|-&lt;br /&gt;
|XmlEncode&lt;br /&gt;
|Encodes the specified string for use in XML.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://blogs.msdn.com/b/cisg/archive/2008/08/28/output-encoding.aspx Output Encoding]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder_methods(v=vs.110).aspx AntiXssEncoder Methods]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184412</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184412"/>
				<updated>2014-10-30T19:04:51Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184409</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184409"/>
				<updated>2014-10-30T18:47:53Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Validating User Input */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184408</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184408"/>
				<updated>2014-10-30T18:31:59Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
*[[ASP.NET Output Encoding]]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184407</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184407"/>
				<updated>2014-10-30T18:29:41Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184406</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184406"/>
				<updated>2014-10-30T18:27:51Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Selectively Disabling Request Validation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail ASP.NET Request Validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184405</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184405"/>
				<updated>2014-10-30T18:26:36Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Validating User Input */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184404</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=184404"/>
				<updated>2014-10-30T18:25:15Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Validating User Input */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, you should not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users and encode the output to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183580</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183580"/>
				<updated>2014-10-12T21:46:39Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Preventing Double Encoding */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, do not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users and encode the output to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encoding Output Values in HTML markup===&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@userInput&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preventing Double Encoding===&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@userInput&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public MvcHtmlString Description { get; set; } // Output encoding is handled manually&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183579</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183579"/>
				<updated>2014-10-12T21:45:30Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* ASP.NET MVC */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, do not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users and encode the output to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encoding Output Values in HTML markup===&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@userInput&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preventing Double Encoding===&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@userInput&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User &lt;br /&gt;
{&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183578</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183578"/>
				<updated>2014-10-12T21:41:32Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* ASP.NET Web API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if any input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, do not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users and encode the output to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encoding Output Values in HTML markup===&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@userInput&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preventing Double Encoding===&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@userInput&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description) {&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User {&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183577</id>
		<title>ASP.NET Request Validation</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=ASP.NET_Request_Validation&amp;diff=183577"/>
				<updated>2014-10-12T21:40:45Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Page rewrite to capture advances in ASP.NET request validation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
''Request validation'' is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content. ASP.NET performs this check because markup or code in the URL query string, cookies, or posted form values might have been added for malicious purposes. This exploit is typically referred to as a ''cross-site scripting'' (XSS) attack.&lt;br /&gt;
Request validation helps prevent this kind of attack. If ASP.NET detects any markup or code in a request, it throws a &amp;quot;potentially dangerous value was detected&amp;quot; error and stops page processing. Request validation is generally desirable and should be left enabled unless you have a specific need that would prevent it from being used.&lt;br /&gt;
&lt;br /&gt;
==Enabling Request Validation==&lt;br /&gt;
Request validation is enabled by default in ASP.NET. You can check to make sure it is not disabled at the page level by searching for any instances of:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You should also inspect the web.config file to ensure that request validation has not been disabled for the entire application:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;pages validateRequest=&amp;quot;false&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET 4.0 request validation is performed for all requests, not just for .aspx page requests. To ensure this is configured correctly look to see that &amp;lt;code&amp;gt;requestValidationMode&amp;lt;/code&amp;gt; has '''NOT''' been set to 2.0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime requestValidationMode=&amp;quot;2.0&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
ASP.NET Web API does not utilize the request validation feature to sanitize user input. You will need to add this protection manually if this input will be used in HTML output. For example if user input is returned to the browser as the result from an AJAX request to a Web API method.&lt;br /&gt;
&lt;br /&gt;
==Validating User Input==&lt;br /&gt;
Even though request validation provides a good level of input protection, do not fully rely on it for securing your application against cross-site scripting attacks. Instead, validate all input from users and encode the output to ensure malicious code is not included.&lt;br /&gt;
&lt;br /&gt;
===Encoding Input Values in Code-Behind===&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.HtmlEncode&amp;lt;/code&amp;gt; to encode user input for use in HTML output:&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedHtmlInput = Server.HtmlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlEncode&amp;lt;/code&amp;gt; to encode user input for use in constructing URLs&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlInput = Server.UrlEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use &amp;lt;code&amp;gt;Server.UrlTokenEncode&amp;lt;/code&amp;gt; to encode user input in byte array form for use as a URL parameter&lt;br /&gt;
&amp;lt;pre&amp;gt;var encodedUrlTokenInput = Server.UrlTokenEncode(userInput);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Encoding Output Values in HTML markup===&lt;br /&gt;
You can HTML encode the value in markup with the &amp;lt;code&amp;gt;&amp;lt;%: %&amp;gt;&amp;lt;/code&amp;gt; syntax, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, in Razor syntax, you can HTML encode with &amp;lt;code&amp;gt;@&amp;lt;/code&amp;gt;, as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;span&amp;gt;@userInput&amp;lt;/span&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Preventing Double Encoding===&lt;br /&gt;
You may run into a scenario where encoding values results in them becoming double encoded on output. ASP.NET provides the &amp;lt;code&amp;gt;HtmlString&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; classes starting with .NET 4.0 which are designed to help. These both implement the &amp;lt;code&amp;gt;IHtmlString&amp;lt;/code&amp;gt; interface which will instruct ASP.NET to not apply encoding within HTML markup when using &amp;lt;code&amp;gt;&amp;lt;%: userInput %&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;@userInput&amp;lt;/code&amp;gt;. Converting a property on your view model from &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;MvcHtmlString&amp;lt;/code&amp;gt; will instruct ASP.NET that HTML encoding has already been accounted for.&lt;br /&gt;
&lt;br /&gt;
===Enhanced Request Validation Encoding===&lt;br /&gt;
ASP.NET request validation uses a black-listing technique that evaluates the string for a set of character combinations that may indicate presence of malicious script. A superior approach is to use a [[Input Validation Cheat Sheet#White_List_Input_Validation|white-listing technique]] for input validation, which can be achieved using the Anti-Cross Site Scripting Library from Microsoft. Starting with ASP.NET 4.5 you can specify that the &amp;lt;code&amp;gt;AntiXssEncoder&amp;lt;/code&amp;gt; from this library be used as the default encoder for you entire application using the &amp;lt;code&amp;gt;encoderType&amp;lt;/code&amp;gt; setting in web.config as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;System.Web.Security.AntiXss.AntiXssEncoder&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you are using a version of .NET earlier than 4.5, you will need to download and include the library as a reference to your project, and then use the earlier library name for the encodeType setting as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;httpRuntime encoderType=&amp;quot;Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary&amp;quot; /&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Selectively Disabling Request Validation==&lt;br /&gt;
In some cases you may need to accept input that will fail request validation, such as when receiving HTML markup from the end user. In these scenarios you should disable request validation for the smallest surface possible, and replace it with your own custom input validation.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
For ASP.NET Web Forms applications you will need to disable request validation at the page level. Be aware that when doing this all input values (cookies, query string, form elements) handled by this page will not be validated by ASP.NET.&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;@ Page ValidateRequest=&amp;quot;false&amp;quot; %&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
To disable request validation for a specific MVC controller action, you can use the &amp;lt;code&amp;gt;[ValidateInput(false)]&amp;lt;/code&amp;gt; attribute as shown below.&lt;br /&gt;
&amp;lt;pre&amp;gt;[ValidateInput(false)]&lt;br /&gt;
public ActionResult Update(int userId, string description) {&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting with ASP.NET MVC 3 you should use the &amp;lt;code&amp;gt;[AllowHtml]&amp;lt;/code&amp;gt; attribute to decorate specific fields on your view model classes where request validation should not be applied:&lt;br /&gt;
&amp;lt;pre&amp;gt;public class User {&lt;br /&gt;
    public int Id { get; set; }&lt;br /&gt;
    public string Name { get; set; }&lt;br /&gt;
    public string Email { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Description { get; set; }&lt;br /&gt;
    [AllowHtml]&lt;br /&gt;
    public string Bio { get; set; }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/hh882339(v=vs.110).aspx Request Validation in ASP.NET]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/System.Web.HttpUtility_methods(v=vs.110).aspx HttpUtility Methods]&lt;br /&gt;
*[http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2 New &amp;lt;%: %&amp;gt; Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.ihtmlstring(v=vs.110).aspx IHtmlString Interface]&lt;br /&gt;
*[http://stackoverflow.com/questions/2293357/what-is-an-mvchtmlstring-and-when-should-i-use-it What is an MvcHtmlString and when should I use it?]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.security.antixss.antixssencoder(v=vs.110).aspx AntiXssEncoder Class]&lt;br /&gt;
*[http://www.microsoft.com/en-sg/download/details.aspx?id=43126 Microsoft Anti-Cross Site Scripting Library V4.3]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.validateinputattribute(v=vs.118).aspx ValidateInputAttribute Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.118).aspx AllowHtmlAttribute Class]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183277</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183277"/>
				<updated>2014-10-03T16:53:36Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Where to Catch Exceptions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application fails while attempting to connect to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Where to Catch Exceptions==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. First and foremost you want to catch exceptions at an application level and ensure they are handled properly. Often this may be the only location necessary to catch exceptions. In addition to this, handling exceptions at lower levels in the application will give you more control and may provide you with more detailed contextual information.&lt;br /&gt;
&lt;br /&gt;
If you use try/catch blocks to handle exceptions be aware of two concerns:&lt;br /&gt;
&lt;br /&gt;
#Avoid using the ''Exception'' class but instead use a descendant class that is the most applicable to the condition you are expecting. &lt;br /&gt;
#Be careful not to just catch an exception and continue processing. Always use the ''throw'' statement to re-throw the exception unless you are certain that this will not leave the application in an unexpected and potentially vulnerable state.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;try&lt;br /&gt;
{&lt;br /&gt;
  Emailer.SendNewUserActivation(this)&lt;br /&gt;
}&lt;br /&gt;
catch (SmtpException ex)&lt;br /&gt;
{&lt;br /&gt;
  this.Delete();&lt;br /&gt;
  throw;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
Logging exception details is important when you need to properly diagnose error conditions with the system. You don't want this detail to be displayed on an error page because it may inadvertently aid a malicious user in an attack. Logging allows your error pages to display a simple generic message alerting end users that an error has occurred, with possibly some options for contacting support. The detail you need for assisting with these issues will be kept securely in the log store.&lt;br /&gt;
&lt;br /&gt;
You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using trace listeners such as the ''TextWriterTraceListener'' configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====HttpResponseException====&lt;br /&gt;
By default any exception thrown within Web API will return an HTTP response status code of 500 (Internal Server Error). For more control over the response you can use ''HttpResponseException'' as shown. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public Product GetProduct(int id)&lt;br /&gt;
{&lt;br /&gt;
    Product item = repository.Get(id);&lt;br /&gt;
    if (item == null)&lt;br /&gt;
    {&lt;br /&gt;
        throw new HttpResponseException(HttpStatusCode.NotFound)&lt;br /&gt;
	  {&lt;br /&gt;
            Content = new StringContent(string.Format(&amp;quot;No product with ID = {0}&amp;quot;, id)),&lt;br /&gt;
            ReasonPhrase = &amp;quot;Product ID Not Found&amp;quot;&lt;br /&gt;
          };&lt;br /&gt;
    }&lt;br /&gt;
    return item;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Web API Global Error Handling (Added in Web API 2.1)====&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183276</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183276"/>
				<updated>2014-10-03T16:44:48Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Web API Global Error Handling (Added in Web API 2.1) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application fails while attempting to connect to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Where to Catch Exceptions==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. First and foremost you want to catch exceptions at an application level and ensure they are handled properly. Often this may be the only location necessary to catch exceptions. In addition to this, handling exceptions at lower levels in the application will give you more control and may provide you with more detailed contextual information.&lt;br /&gt;
&lt;br /&gt;
If you use try/catch blocks to handle exceptions be aware of two concerns:&lt;br /&gt;
&lt;br /&gt;
#Avoid using the ''Exception'' class but instead use a descendant class this the most applicable to the condition you are expecting. This may require you to write your own descendant class.&lt;br /&gt;
#Be careful not to catch an exception and continue processing. Always use the ''throw'' statement to re-throw the exception unless you are certain that this will not leave the application in an unexpected and potentially vulnerable state.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;try&lt;br /&gt;
{&lt;br /&gt;
  Emailer.SendNewUserActivation(this)&lt;br /&gt;
}&lt;br /&gt;
catch (SmtpException ex)&lt;br /&gt;
{&lt;br /&gt;
  this.Delete();&lt;br /&gt;
  throw;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
Logging exception details is important when you need to properly diagnose error conditions with the system. You don't want this detail to be displayed on an error page because it may inadvertently aid a malicious user in an attack. Logging allows your error pages to display a simple generic message alerting end users that an error has occurred, with possibly some options for contacting support. The detail you need for assisting with these issues will be kept securely in the log store.&lt;br /&gt;
&lt;br /&gt;
You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using trace listeners such as the ''TextWriterTraceListener'' configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====HttpResponseException====&lt;br /&gt;
By default any exception thrown within Web API will return an HTTP response status code of 500 (Internal Server Error). For more control over the response you can use ''HttpResponseException'' as shown. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public Product GetProduct(int id)&lt;br /&gt;
{&lt;br /&gt;
    Product item = repository.Get(id);&lt;br /&gt;
    if (item == null)&lt;br /&gt;
    {&lt;br /&gt;
        throw new HttpResponseException(HttpStatusCode.NotFound)&lt;br /&gt;
	  {&lt;br /&gt;
            Content = new StringContent(string.Format(&amp;quot;No product with ID = {0}&amp;quot;, id)),&lt;br /&gt;
            ReasonPhrase = &amp;quot;Product ID Not Found&amp;quot;&lt;br /&gt;
          };&lt;br /&gt;
    }&lt;br /&gt;
    return item;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Web API Global Error Handling (Added in Web API 2.1)====&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183275</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183275"/>
				<updated>2014-10-03T16:44:14Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* ASP.NET Web API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application fails while attempting to connect to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Where to Catch Exceptions==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. First and foremost you want to catch exceptions at an application level and ensure they are handled properly. Often this may be the only location necessary to catch exceptions. In addition to this, handling exceptions at lower levels in the application will give you more control and may provide you with more detailed contextual information.&lt;br /&gt;
&lt;br /&gt;
If you use try/catch blocks to handle exceptions be aware of two concerns:&lt;br /&gt;
&lt;br /&gt;
#Avoid using the ''Exception'' class but instead use a descendant class this the most applicable to the condition you are expecting. This may require you to write your own descendant class.&lt;br /&gt;
#Be careful not to catch an exception and continue processing. Always use the ''throw'' statement to re-throw the exception unless you are certain that this will not leave the application in an unexpected and potentially vulnerable state.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;try&lt;br /&gt;
{&lt;br /&gt;
  Emailer.SendNewUserActivation(this)&lt;br /&gt;
}&lt;br /&gt;
catch (SmtpException ex)&lt;br /&gt;
{&lt;br /&gt;
  this.Delete();&lt;br /&gt;
  throw;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
Logging exception details is important when you need to properly diagnose error conditions with the system. You don't want this detail to be displayed on an error page because it may inadvertently aid a malicious user in an attack. Logging allows your error pages to display a simple generic message alerting end users that an error has occurred, with possibly some options for contacting support. The detail you need for assisting with these issues will be kept securely in the log store.&lt;br /&gt;
&lt;br /&gt;
You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using trace listeners such as the ''TextWriterTraceListener'' configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====HttpResponseException====&lt;br /&gt;
By default any exception thrown within Web API will return an HTTP response status code of 500 (Internal Server Error). For more control over the response you can use ''HttpResponseException'' as shown. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public Product GetProduct(int id)&lt;br /&gt;
{&lt;br /&gt;
    Product item = repository.Get(id);&lt;br /&gt;
    if (item == null)&lt;br /&gt;
    {&lt;br /&gt;
        throw new HttpResponseException(HttpStatusCode.NotFound)&lt;br /&gt;
	  {&lt;br /&gt;
            Content = new StringContent(string.Format(&amp;quot;No product with ID = {0}&amp;quot;, id)),&lt;br /&gt;
            ReasonPhrase = &amp;quot;Product ID Not Found&amp;quot;&lt;br /&gt;
          };&lt;br /&gt;
    }&lt;br /&gt;
    return item;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183274</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183274"/>
				<updated>2014-10-03T16:24:00Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Including &amp;quot;Where to Catch Exceptions&amp;quot; section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application fails while attempting to connect to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Where to Catch Exceptions==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. First and foremost you want to catch exceptions at an application level and ensure they are handled properly. Often this may be the only location necessary to catch exceptions. In addition to this, handling exceptions at lower levels in the application will give you more control and may provide you with more detailed contextual information.&lt;br /&gt;
&lt;br /&gt;
If you use try/catch blocks to handle exceptions be aware of two concerns:&lt;br /&gt;
&lt;br /&gt;
#Avoid using the ''Exception'' class but instead use a descendant class this the most applicable to the condition you are expecting. This may require you to write your own descendant class.&lt;br /&gt;
#Be careful not to catch an exception and continue processing. Always use the ''throw'' statement to re-throw the exception unless you are certain that this will not leave the application in an unexpected and potentially vulnerable state.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;try&lt;br /&gt;
{&lt;br /&gt;
  Emailer.SendNewUserActivation(this)&lt;br /&gt;
}&lt;br /&gt;
catch (SmtpException ex)&lt;br /&gt;
{&lt;br /&gt;
  this.Delete();&lt;br /&gt;
  throw;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
Logging exception details is important when you need to properly diagnose error conditions with the system. You don't want this detail to be displayed on an error page because it may inadvertently aid a malicious user in an attack. Logging allows your error pages to display a simple generic message alerting end users that an error has occurred, with possibly some options for contacting support. The detail you need for assisting with these issues will be kept securely in the log store.&lt;br /&gt;
&lt;br /&gt;
You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using trace listeners such as the ''TextWriterTraceListener'' configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183273</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183273"/>
				<updated>2014-10-03T15:41:57Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Displaying Errors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application fails while attempting to connect to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described in this section that are applicable to your application. You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using ''trace listeners'' such as the TextWriterTraceListener configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183272</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183272"/>
				<updated>2014-10-03T15:39:30Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described in this section that are applicable to your application. You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using ''trace listeners'' such as the TextWriterTraceListener configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.diagnostics.trace(v=vs.110).aspx Trace Class]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183271</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183271"/>
				<updated>2014-10-03T15:36:28Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Logging Exception Details */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described in this section that are applicable to your application. You can use several methods to log exception events, including popular third party libraries: log4net, nlog, Common.Logging, etc. An out-of-the-box solution included with .NET is System.Diagnostics Tracing as shown in the code examples in this article. These trace messages can then be written out using ''trace listeners'' such as the TextWriterTraceListener configured here.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;system.diagnostics&amp;gt;    &lt;br /&gt;
  &amp;lt;trace autoflush=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;listeners&amp;gt;&lt;br /&gt;
      &amp;lt;add name=&amp;quot;EventTracer&amp;quot; &lt;br /&gt;
        type=&amp;quot;System.Diagnostics.TextWriterTraceListener, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&amp;quot; &lt;br /&gt;
        initializeData=&amp;quot;&amp;lt;app root directory&amp;gt;\eventtrace.log&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/listeners&amp;gt;&lt;br /&gt;
  &amp;lt;/trace&amp;gt;&lt;br /&gt;
&amp;lt;/system.diagnostics&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One benefit to using System.Diagnostics Tracing in your code is that most logging frameworks already support including these events and writing them to a variety of output types. &lt;br /&gt;
&lt;br /&gt;
For detailed security guidance specific to logging see [[Reviewing Code for Logging Issues]]. &lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183268</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183268"/>
				<updated>2014-10-03T14:34:00Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: Include warning on use of Server.ClearError&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described below as are applicable to your application.&lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server.ClearError===&lt;br /&gt;
Use the ''Server.ClearError()'' method with caution when handling exceptions. This method will clear the exception state and allow execution to continue rather than displaying an error message to the end user. There are valid scenarios where this is needed, but in general you should avoid this as it could allow a malicious user to bypass security controls if they are somehow able to force an error condition.&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183220</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183220"/>
				<updated>2014-10-02T13:59:48Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* Application Level */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described below as are applicable to your application.&lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax.cs file that will catch and log all unhandled ASP.NET errors at the application level. In this example ''Server.ClearError()'' is not called so that the exception will continue to be processed by the error display mechanism defined by the ''&amp;lt;customErrors&amp;gt;'' element in web.config. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
&lt;br /&gt;
  // Calling Server.ClearError allows the page to continue processing,&lt;br /&gt;
  // otherwise control is passed to application level error handling.&lt;br /&gt;
  Server.ClearError();&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183219</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183219"/>
				<updated>2014-10-02T13:58:23Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* HTTP Status Codes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns an HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described below as are applicable to your application.&lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax file that will catch and log all unhandled ASP.NET errors at the application level. In this example ''Server.ClearError()'' is not called so that the exception will continue to be processed by the error display mechanism defined by the ''&amp;lt;customErrors&amp;gt;'' element in web.config. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
&lt;br /&gt;
  // Calling Server.ClearError allows the page to continue processing,&lt;br /&gt;
  // otherwise control is passed to application level error handling.&lt;br /&gt;
  Server.ClearError();&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183218</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183218"/>
				<updated>2014-10-02T13:51:56Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: /* HTTP Status Codes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter and returns a HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. One solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described below as are applicable to your application.&lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax file that will catch and log all unhandled ASP.NET errors at the application level. In this example ''Server.ClearError()'' is not called so that the exception will continue to be processed by the error display mechanism defined by the ''&amp;lt;customErrors&amp;gt;'' element in web.config. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
&lt;br /&gt;
  // Calling Server.ClearError allows the page to continue processing,&lt;br /&gt;
  // otherwise control is passed to application level error handling.&lt;br /&gt;
  Server.ClearError();&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183217</id>
		<title>Exception Handling</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Exception_Handling&amp;diff=183217"/>
				<updated>2014-10-02T13:50:17Z</updated>
		
		<summary type="html">&lt;p&gt;Bricex: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT DOCUMENT - WORK IN PROGRESS =&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
An exception is any error condition or unexpected behavior that is encountered by your application. There are several key security concerns to be aware of when handling exceptions in .NET web applications:&lt;br /&gt;
*Preventing system information leakage that could aid a malicious user.&lt;br /&gt;
*Ensuring the application fails securely under all circumstances, both expected and not expected.&lt;br /&gt;
*Using a centralized error strategy to reduce points of failure and promote consistency.&lt;br /&gt;
*Log when exceptions are thrown and include sufficient detail for security auditing.&lt;br /&gt;
&lt;br /&gt;
==Displaying Errors==&lt;br /&gt;
When your application displays error messages, it should not give away information that a malicious user might find helpful in attacking your system. For example, if your application unsuccessfully tries to log in to a database, it should not display an error message that includes the connection string being used.&lt;br /&gt;
&lt;br /&gt;
The default display mechanism for unhandled exceptions in ASP.NET is controlled via the ''&amp;lt;customErrors&amp;gt;'' element in the web.config file. This element includes a ''mode'' attribute that can be set to: ''Off'', ''On'', or ''RemoteOnly''. This mode setting should never be set to ''Off'' in a production environment because it will cause exception details to be displayed to the end user which may include sensitive information about the system.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;customErrors mode=&amp;quot;RemoteOnly&amp;quot; defaultRedirect=&amp;quot;/Error&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;404&amp;quot; redirect=&amp;quot;/Error?id=404&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;error statusCode=&amp;quot;403&amp;quot; redirect=&amp;quot;/Error?id=403&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/customErrors&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===HTTP Status Codes===&lt;br /&gt;
When reviewing error responses, be careful that a malicious user is not able to glean any potentially sensitive information about the system from the status code returned. For example: an API search that takes a tenant ID parameter that returns a HTTP 404 response for no results, and an HTTP 500 response if the tenant ID does not exist. An attacker could potentially leverage this information to determine if a tenant ID is valid or not. For this scenario one solution would be to catch the exception thrown when a supplied tenant ID does not exist, and return an HTTP 404 status code with the error response.&lt;br /&gt;
&lt;br /&gt;
==Logging Exception Details==&lt;br /&gt;
There are a variety of places within a typical web application where exceptions can be caught and logged. Ideally, you should handle exceptions in as many of the locations described below as are applicable to your application.&lt;br /&gt;
&lt;br /&gt;
===Application Level===&lt;br /&gt;
The following code example shows how to create an error handler in the Global.asax file that will catch and log all unhandled ASP.NET errors at the application level. In this example ''Server.ClearError()'' is not called so that the exception will continue to be processed by the error display mechanism defined by the ''&amp;lt;customErrors&amp;gt;'' element in web.config. Unhandled exceptions must be logged within ''Application_Error'' rather than in a subsequent page defined by ''&amp;lt;customErrors&amp;gt;'', because the exception details are only available at this point.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Application_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web Forms===&lt;br /&gt;
ASP.NET web form pages provide the ability to capture exceptions at the page level in a similar fashion to the application level. This may be useful if you need to perform failure handling logic or log additional detail unique to the page.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;void Page_Error(object sender, EventArgs e)&lt;br /&gt;
{&lt;br /&gt;
  // Get the exception object.&lt;br /&gt;
  var exception = Server.GetLastError();&lt;br /&gt;
&lt;br /&gt;
  // Log exception details. Be sure to include items like client IP &lt;br /&gt;
  // address and current authenticated user if applicable.&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(exception.ToString());&lt;br /&gt;
&lt;br /&gt;
  // Calling Server.ClearError allows the page to continue processing,&lt;br /&gt;
  // otherwise control is passed to application level error handling.&lt;br /&gt;
  Server.ClearError();&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET MVC===&lt;br /&gt;
The MVC Controller class provides an ''OnException'' method similar to the ''Page_Error'' method in ASP.NET Web Forms. It allows you to process all unhandled exceptions thrown by actions of the controller. One option for implementing exception logging is to create a base controller class and defining the ''OnException'' method in this one central location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;protected override void OnException(ExceptionContext filterContext)&lt;br /&gt;
{&lt;br /&gt;
  base.OnException(filterContext);&lt;br /&gt;
  System.Diagnostics.Trace.TraceError(filterContext.Exception.ToString());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A second (and arguably better) approach is to utilize the ''System.Web.Mvc.HandleErrorAttribute'' which can be used to handle exceptions at the application, controller, or action level. To apply this at the application level you add an instance of ''HandleErrorAttribute'' to the ''GlobalFilterCollection'' at application startup. This takes affect before the ''Application_Error'' method in Global.asax.cs, and by default will redirect to the ''Error'' view in the ~/Views/Shared folder. This will bypass any custom error pages defined via the ''&amp;lt;customErrors&amp;gt;'' element in web.config.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public static void RegisterGlobalFilters(GlobalFilterCollection filters)&lt;br /&gt;
{&lt;br /&gt;
  filters.Add(new HandleErrorAttribute());&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can log the exception details right within the ''Error.cshtml'' view using properties from the view model which is of type ''System.Web.Mvc.HandleErrorInfo''.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@model System.Web.Mvc.HandleErrorInfo&lt;br /&gt;
&lt;br /&gt;
@{&lt;br /&gt;
    ViewBag.Title = &amp;quot;Error&amp;quot;;&lt;br /&gt;
    &lt;br /&gt;
    // Log exception details&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(Model.Exception.ToString());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h1 class=&amp;quot;text-danger&amp;quot;&amp;gt;Error.&amp;lt;/h1&amp;gt;&lt;br /&gt;
&amp;lt;h2 class=&amp;quot;text-danger&amp;quot;&amp;gt;An error occurred while processing your request.&amp;lt;/h2&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===ASP.NET Web API===&lt;br /&gt;
When handling exceptions for your API endpoints you generally want to return a JSON or XML response rather than redirect to an HTML page. ASP.NET Web API provides exception filters to customize how exceptions are handled. A good way of adding exception logging is to derive your own class from ''System.Web.Http.Filters.ExceptionFilterAttribute'' and override the ''OnException'' method as shown.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class LogExceptionFilterAttribute : ExceptionFilterAttribute &lt;br /&gt;
{&lt;br /&gt;
  public override void OnException(HttpActionExecutedContext context)&lt;br /&gt;
  {&lt;br /&gt;
    System.Diagnostics.Trace.TraceError(context.Exception.ToString());&lt;br /&gt;
    base.OnException(context);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can apply the ''[LogExceptionFilter]'' attribute to any ''ApiController'', or add it at a global level using the following code at application startup.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Configuration.Filters.Add(new LogExceptionFilterAttribute());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Web API Global Error Handling (Added in Web API 2.1)===&lt;br /&gt;
There are a number of cases in Web API where an exception can occur that falls outside of what exception filters can handle. These include exceptions in controller construction, message handlers, routing, and response content serialization. To handle these exceptions you will need to register a class that implements the ''IExceptionLogger'' interface, or derives from the ''ExceptionLogger'' base class. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public interface IExceptionLogger&lt;br /&gt;
{&lt;br /&gt;
  Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You then register your class at application startup to ensure that all exceptions raised during a Web API request life cycle are captured and logged. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;System.Web.Http.GlobalConfiguration.Services.Add(typeof(IExceptionLogger), new MyExceptionLogger());&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Logging Sensitive Data==&lt;br /&gt;
Be careful when logging exception details that you do not inadvertently expose sensitive data to a logging system or external reports that may have relaxed access controls compared to the primary system. For example if a ''SqlException'' is thrown and the details contain a connection string, you may want to mask the password attribute before logging the event. Use care when logging exception details that may contain the following types of data:&lt;br /&gt;
&lt;br /&gt;
*Passwords&lt;br /&gt;
*Authentication tokens&lt;br /&gt;
*Personally identifiable information (PII)&lt;br /&gt;
*Personal health information (PHI)&lt;br /&gt;
*Credit card data&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs Displaying a Custom Error Page]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/h0hfz6fc(v=vs.100).aspx customErrors Element (ASP.NET Settings Schema)]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/ed577840(v=vs.100).aspx How to: Handle Page-Level Errors]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception(v=vs.118).aspx Controller.OnException Method]&lt;br /&gt;
*[http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx HandleErrorAtttribute Class]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/exception-handling Exception Handling in ASP.NET Web API]&lt;br /&gt;
*[http://www.asp.net/web-api/overview/testing-and-debugging/web-api-global-error-handling Web API 2 .1 Global Error Handling]&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP .NET Project]][[Category:Stub]]&lt;/div&gt;</summary>
		<author><name>Bricex</name></author>	</entry>

	</feed>