Topic Index
There is a tremendous amount of information available about security issues on the Web. In particular, one could spend weeks going over the information on the Microsoft site
Designing for the Web adds a number of security concerns.
Microsoft has developed Design Guidelines for Secure Web Applications to guide the ASP.NET developer. These guidelines are organized by categories where mistakes are made most often.
While no panacea, managed programming languages like Java and C# have reduced the risk on several programming errors which lead to writing insecure code. (See Coding Horror's Sins of Software Security).
Likewise, use of the ASP.NET framework has reduced the exposure to writing insecure Web applications.
Best practices with input validation:
One of the most powerful ways to constrain data input is through validation controls and regular expressions
Some good references on the Microsoft site for input validation include
A frequent security concern with input is code injection and cross site scripting. A good Web paper addressing the subject is HTML Code Injection and Cross-site scripting.
The .net platform provides HttpUtility.HtmlEncode(string) as a way to prevent malicious scripts from being displayed in the browser.
Hands-on Exercise
<script>prompt('Please enter your password')</script>
By default, ASP.NET protects against Cross Site Scripting code injections via text boxes. For purposes of illustration, or if you want to allow users to be able to enter script, you can set the validateRequest property in your Page directive to false.
validateRequest="false"
To protect against this type of risk use
HttpUtility.HtmlEncode(TextBox1.Text)
Microsoft Microsoft Released a new Anti-XSS tool that follows an accept-only approach. That is, instead of filtering out bad input,this tool accepts only a finite set of valid input and everything else is considered invalid.
To use the tool in your project, you will need to download and include the AntiXSSLibrary.dll in the bin folder of your project.
In How To: Prevent Cross-Site Scripting in ASP.NET there are two things that need to be done:
HttpUtility.HtmlEncode when generating html elements based on user input
SQL injection can occur when an application uses input to construct dynamic SQL statements or when it uses stored procedures to connect to the database. In How To: Protect From SQL Injection in ASP.NET, Microsoft recommends
To give you a feel for this, add a simple one table SQL Server express database to your website and use it to store usernames and passwords.
To hack into this login page, use
hacker' or 1=1;--
Configuration files, such as the Web.config, are often used to hold sensitive information, including user names, passwords, database connection strings, and encryption keys.
If you do not protect this information, your application is vulnerable to attackers or malicious users obtaining sensitive information such as account user names and passwords, database names and server names.
Encrypting and decrypting data incurs performance overhead. To keep this overhead to a minimum, encrypt only the sections of your configuration file that store sensitive data.
.NET 2.0 includes Aspnet_Regiis.exe tool that uses the Windows Data Protection API (DPAPI) to encrypt sections of your configuration file. ASP.NET automatically decrypts configuration sections when processing them; therefore, you do not need to write any additional decryption code.
To encrypt the connectionStrings section of Web.config stored at u:\Day37, use the command line promt to change directories to cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 using the correct .NET Framework version number. then type
aspnet_regiis.exe -pef "connectionStrings" u:\Day37
which changes
into
Detailed instructions are available at How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI
More information is available at Encrypting Configuration Information in ASP.NET 2.0 Applications
Rather than using the aspnet_regiis.exe tool, you can also do this through code.
See Encrypt Connection Strings AppSettings and Web.Config in ASP.NET 2.0 - Security Best Practices
Page last modified on May 28, 2008, at 11:34 AM