The topic of creating a secure Web application is extensive. It requires study to understand security vulnerabilities. You also need to familiarize yourself with the security facilities of Windows, the .NET Framework, and ASP.NET. Finally, it is essential to understand how to use these security features to counter threats.
Even if you are not experienced with security, there are basic measures that you should take to protect your Web application. The following list provides minimum-security guidelines that apply to all Web applications and that you should follow:
Even the most elaborate application security can fail if a malicious user can use simple ways to get to your computers. Follow these guidelines:
Back up often and keep your backups physically secure.
Keep your Web server computer physically secure so that unauthorized users cannot get to it, turn it off, or take it.
Use the Windows NTFS file system, not FAT32. NTFS offers substantially more security than FAT32. For details, see the Windows documentation.
Secure the Web server computer and all computers on the same network with strong passwords.
Close unused ports and turn off unused services.
Run a virus checker that monitors inbound and outbound traffic.
Establish and enforce a policy that forbids users from keeping their passwords written down in an easy-to-find location.
Use Windows event logging and examine the logs frequently for suspicious activity. This includes repeated attempts to log on to your system and an extremely high number of requests against your Web server.
When your application runs, it runs within a context that has specific privileges on the local computer and potentially on remote computers. For information on configuring the application identity, see . To run with least privileges, follow these guidelines:
Do not run your application with the identity of a system user (administrator).
Run the application in the context of a user with the minimum practical privileges.
Set permissions (Access Control Lists or ACLs) on all the resources required for your application. Use the least permissive setting. For example, if practical in your application, set files to be read-only. For a list of the minimum required ACL permissions required for the identity of your ASP.NET application, see .
Keep files for your Web application in a folder below the application root. Do not allow users the option of specifying a path for any file access in your application. This helps prevent users from getting access to the root of your server.
In many applications, users access the site anonymously (without having to provide credentials). If so, your application accesses resources by running in the context of a predefined user. By default, this context is the local ASPNET user (on Windows 2000 or Windows XP) or NETWORK SERVICE user (on Windows Server 2003) on the Web server computer. To restrict access to users who are authenticated, follow these guidelines:
If your application is an intranet application, configure it to use Windows integrated security. That way, the user's logon credentials can be used to access resources. For more information, see .
If you need to gather credentials from the user, use one of the ASP.NET authentication strategies. For an example, see .
As a general rule, never assume that input you get from users is safe. It is easy for malicious users to send potentially dangerous information from the client to your application. To guard against malicious input, follow these guidelines:
In ASP.NET Web pages, filter user input to check for HTML tags, which might contain script. For details, see .
Never echo (display) unfiltered user input. Before displaying untrusted information, encode HTML to turn potentially harmful script into display strings.
Never store unfiltered user input in a database.
If you want to accept some HTML from a user, filter it manually. In your filter, explicitly define what you will accept. Do not create a filter that tries to filter out malicious input; it is very difficult to anticipate all possible malicious input.
Do not assume that information you get from the HTTP request header (in the object) is safe. Use safeguards for query strings, cookies, and so on. Be aware that information the browser reports to the server (user agent information) can be spoofed, in case that is important in your application.
If possible, do not store sensitive information in a place accessible from the browser, such as hidden fields or cookies. For example, do not store a password in a cookie.
View state is stored in a hidden field in an encoded format. By default, it includes a message authentication code (MAC) so that the page can determine whether view state was tampered with. If sensitive information is stored in view state, encrypt by setting the page's property to true.
Databases typically have their own security. An important aspect of a secure Web application is designing a way for the application to access the database securely. Follow these guidelines:
Use the inherent security of your database to limit who can access database resources. The exact strategy depends on your database and your application:
If practical in your application, use integrated security so that only Windows-authenticated users can access the database. Integrated security is more secure than passing explicit credentials to the database.
If your application involves anonymous access, create a single user with very limited permissions, and perform queries by connecting as this user.
Do not create SQL statements by concatenating strings that involve user input. Instead, create a parameterized query and use user input to set parameter values.
If you must store a user name and password somewhere to use as the database login credentials, store them in the Web.config file and secure the file with protected configuration. For details, see .
For more information on accessing data securely, see and .
If you are not careful, a malicious user can deduce important information about your application from the error messages it displays. Follow these guidelines:
Do not write error messages that echo information that might be useful to malicious users, such as a user name.
Configure the application not to show detailed errors to users. If you want to display detailed error messages for debugging, determine first whether the user is local to the Web server. For details, see .
Create custom error handling for situations that are prone to error, such as database access. For more information, see .