title: ASP.NET Security Best Practices
body: <p>What are others ASP.NET Security Best Practices?</p>
  
  <p>So far identified are listed here:</p>
  
  <ul>
  <li><p>Always generate new encryption keys and admin passwords whenever you are moving an application to production.</p></li>
  <li><p>Never store passwords directly or in encrypted form. Always stored one way hashed passwords.</p></li>
  <li><p>Always store connection strings in  tag of Web.config and encrypt it in configuration section by using protected configuration providers (RSA or DPAPI). See <a href="http://msdn.microsoft.com/en-us/library/ms998372.aspx#pagpractices0001_conhowtoencryptsensitivedatainmachineconfigandwebconfig" rel="noreferrer">example here</a></p></li>
  <li><p>Use user ID with least-privilege to connect to SQL server or the database you are using. E.g if you are only executing stored procedures from a certain module of application then you must create a user ID which has permissions to execute only.</p></li>
  <li><p>Use <a href="http://msdn.microsoft.com/en-us/library/system.security.permissions.principalpermission.aspx" rel="noreferrer">PrincipalPermission</a> if you want to use role-base security on pages.</p>
  
  <pre>[PrincipalPermission(SecurityAction.Demand, Role="Admin")]  
  public class AdminOnlyPage : BasePageClass  
  {  
    // ...  
  }</pre></li>
  <li><p>Always use parameters to prevent <a href="http://en.wikipedia.org/wiki/SQL_Injection" rel="noreferrer">SQL Injection</a> in the SQL queries.</p>
  
  <ol>
  <li>Consider installing URLScan on your IIS servers to protect against SQL Injection.
  Also, for protecting against XSS attacks. You can use MSFT's AntiXSS library instead of the built to encode output instead of the built in HtmlEncode found in HttpServerUtility.   </li>
  </ol></li>
  <li><p>Always keep on customErrors in web config to make you errors/exceptions private  </p>
  
  <p>&lt;customErrors mode="On" defaultRedirect="MyErrorPage.htm" /&gt;</p></li>
  <li><p>In web applications, always validate the user's inputs for html tags or any scripts.</p></li>
  <li><p>Never store sensitive information, like passwords in cookies.</p></li>
  <li>Don't display system error messages, stack traces etc, in case of exception.</li>
  </ul>