Print this page Share

Beginning ASP.NET Security

ISBN: 978-0-470-74365-2
436 pages
March 2010
Beginning ASP.NET Security (0470743654) cover image
Programmers: protect and defend your Web apps against attack!

You may know ASP.NET, but if you don't understand how to secure your applications, you need this book. This vital guide explores the often-overlooked topic of teaching programmers how to design ASP.NET Web applications so as to prevent online thefts and security breaches.

You'll start with a thorough look at ASP.NET 3.5 basics and see happens when you don't implement security, including some amazing examples. The book then delves into the development of a Web application, walking you through the vulnerable points at every phase. Learn to factor security in from the ground up, discover a wealth of tips and industry best practices, and explore code libraries and more resources provided by Microsoft and others.

  • Shows you step by step how to implement the very latest security techniques
  • Reveals the secrets of secret-keeping—encryption, hashing, and not leaking information to begin with
  • Delves into authentication, authorizing, and securing sessions
  • Explains how to secure Web servers and Web services, including WCF and ASMX
  • Walks you through threat modeling, so you can anticipate problems
  • Offers best practices, techniques, and industry trends you can put to use right away

Defend and secure your ASP.NET 3.5 framework Web sites with this must-have guide.

See More


Chapter 1 Why Web Security Matters.

Anatomy of an Attack.

Risks and Rewards.

Building Security from the Ground Up.

Defense in Depth.

Never Trust Input.

Fail Gracefully.

Watch for Attacks.

Use Least Privilege.

Firewalls and Cryptography Are Not a Panacea.

Security Should Be Your Default State.

Code Defensively.

The OWASP Top Ten.

Moving Forward.



Chapter 2 How the Web Works.

Examining the HTTP.

Requesting a Resource.

Responding to a Request.

Sniffing HTTP Requests and Responses.

Understanding HTML Forms.

Examining How ASP.NET Works.

Understanding How ASP.NET Events Work.

Examining the ASP.NET Pipeline.

Writing HTTP Modules.


Chapter 3 Safely Accepting User Input.

Defining Input.

Dealing with Input Safely.

Echoing User Input Safely.

Mitigating Against XSS.

The Microsoft Anti-XSS Library.

The Security Run-Time Engine.

Constraining Input.

Protecting Cookies.

Validating Form Input.

Validation Controls.

Standard ASP.NET Validation Controls.

Using the RequiredFileValidator.

Using the RangeValidator.

Using the RegularExpressionValidator.

Using the CompareValidator.

Using the CustomValidator.

Validation Groups.

A Checklist for Handling Input.

Chapter 4 Using Query Strings, Form Fields, Events, and Browser Information.

Using the Right Input Type.

Query Strings.

Form Fields.

Request Forgery and How to Avoid It.

Mitigating Against CSRF.

Protecting ASP.NET Events.

Avoiding Mistakes with Browser Information.

A Checklist for Query Strings, Forms, Events, and Browser Information.

Chapter 5 Controlling Information.

Controlling ViewState.

Validating ViewState.

Encrypting ViewState.

Protecting Against ViewState One-Click Attacks.

Removing ViewState from the Client Page.

Disabling Browser Caching.

Error Handling and Logging.

Improving Your Error Handling.

Watching for Special Exceptions.

Logging Errors and Monitoring Your Application.

Using the Windows Event Log.

Using Email to Log Events.

Using ASP.NET Tracing.

Using Performance Counters.

Using WMI Events.

Another Alternative: Logging Frameworks.

Limiting Search Engines.

Controlling Robots with a Metatag.

Controlling Robots with robots.txt.

Protecting Passwords in Config Files.

A Checklist for Query Strings, Forms, Events and Browser Information.

Chapter 6 Keeping Secrets Secret — Hashing and Encryption.

Protecting Integrity with Hashing.

Choosing a Hashing Algorithm.

Protecting Passwords with Hashing.

Salting Passwords.

Generating Secure Random Numbers.

Encrypting Data.

Understanding Symmetric Encryption.

Protecting Data with Symmetric Encryption.

Sharing Secrets with Asymmmetric Encryption.

Using Asymmetric Encryption without Certificates.

Using Certificates for Asymmetric Encryption.

Getting a Certificate.

Using the Windows DPAPI.

A Checklist for Encryption.


Chapter 7 Adding Usernames and Passwords.

Authentication and Authorization.

Discovering Your Own Identity.

Adding Authentication in ASP.NET.

Using Forms Authentication.

Windows Authentication.

Authorization in ASP.NET.

Examining <allow> and <deny>.

Role-Based Authorization.

Limiting Access to Files and Folders.

Checking Users and Roles Programmatically.

A Checklist for Authentication and Authorization.

Chapter 8 Securely Accessing Databases.

Writing Bad Code: Demonstrating SQL Injection.

Fixing the Vulnerability.

More Security for SQL Server.

Connecting Without Passwords.

SQL Permissions.

Using Views.

SQL Express User Instances.

Drawbacks for the VS Built-in Web Server.

Dynamic SQL Stored Procedures.

Using SQL Encryption.

A Checklist for Securely Accessing Databases.

Chapter 9 Using the File System.

Accessing Existing Files Safely.

Making Static Files Secure.

Making a File Downloadable and Setting Its Name.

Adding Further Checks to File Access.

Accessing Files on a Remote System.

Creating Files Safely.

Handling User Uploads.

Using the File Upload Control.

A Checklist for Securely Accessing Files.

Chapter 10 Securing XML.

Validating XML.

Well-Formed XML.

Valid XML.

XML Parsers.

Querying XML.

Avoiding XPath Injection.

Securing XML Documents.

Encrypting XML Documents.

Signing XML Documents.

A Checklist for XML.


Chapter 11 Sharing Data with Windows Communication Foundation.

Creating and Consuming WCF Services.

Security and Privacy with WCF.

Adding Security to an Internet Service.

Signing Messages with WCF.

Logging and Auditing in WCF.

Validating Parameters Using Inspectors.

Using Message Inspectors.

Throwing Errors in WCF.

A Checklist for Securing WCF.

Chapter 12 Securing Rich Internet Applications.

RIAU Architecture.

Security in Ajax Applications.

The XMLHttpRequest Object.

The Ajax Same Origin Policy.

The Microsoft ASP.NET Ajax Framework.

Security in Silverlight Applications.

Understanding the CoreCLR Security Model.

Using the HTML Bridge.

Accessing the Local File System.

Using Cryptography in Sliverlight.

Accessing the Web and Web Services with Silverlight.

Using ASP.NET Authentication and Authorization in Ajax and Silverlight.

A Checklist for Securing Ajax and Silverlight.


Understanding Code Access Security.

Using ASP.NET Trust Levels.

Demanding Minimum CAS Permissions.

Asking and Checking for CAS Permissions.

Testing Your Application Under a New Trust Level.

Using the Global Assembly Cache to Run Code Under Full Trust.

.NET 4 Changes for Trust and ASP.NET.

A Checklist for Code not Under Full Trust.


Installing and Configuring IIS7.

IIS Role Services.

Removing Global Features for an Individual Web Site.

Creating and Configuring Application Pools.

Configuring Trust Levels in IIS.

Locking Trust Levels.

Creating Custom Trust Levels.

Filtering Requests.

Using Log Parser to Mine IIS Log Files.

Using Certificates.

Requesting an SSL Certificate.

Configuring a Site to Use HTTPS.

Setting up a Test Certification Authority.

A Checklist for Securing Internet Information Server (IIS).

Chapter 15 Third-Party Authentication.

A Brief History of Federated Identity.

Using the Windows Identity Foundation to Accept SAML and Information Cards.

Creating a “Claims-Aware” Web Site.

Accepting Information Cards.

Working with a Claims Identity.

Using OpenID with Your Web Site.

Using Windows Live ID with Your Web Site.

A Strategy for Integrating Third-Party Authentication with Forms Authentication.


Chapter 16 Secure Development with the ASP.NET MVC Framework.

MVC Input and Output.

Protecting Yourself Against XSS.

Protecting an MBC Application Against CSRF.

Securing Model Binding.

Providing Validation for and Error Messages from Your Model.

Authentication and Authorization with ASP.NET MVC.

Authorizing Actions and Controllers.

Protecting Public Controller Methods.

Discovering the Current User.

Customizing Authorization with an Authorization Filter.

Error Handling with ASP.NET MVC.

A Checklist for Secure Development with the ASP.NET MVC Framework.


See More
Barry Dorrans is a consultant, speaker and Microsoft MVP in the "Visual Tools – Security" category. His approach to development and writing blends humor with the paranoia suitable for considering security.
See More
Download TitleSizeDownload
Chapter 2 16.97 KB Click to Download
Chapter 3 22.66 KB Click to Download
Chapter 5 3.93 KB Click to Download
Chapter 6 17.12 KB Click to Download
Chapter 7 3.83 MB Click to Download
Chapter 8 166.52 KB Click to Download
Chapter 9 7.46 KB Click to Download
Chapter 10 205.62 KB Click to Download
Chapter 11 5.75 MB Click to Download
Chapter 12 171.26 KB Click to Download
Chapter 15 49.50 KB Click to Download
See More

Do you think you've discovered an error in this book? Please check the list of errata below to see if we've already addressed the error. If not, please submit the error via our Errata Form. We will attempt to verify your error; if you're right, we will post a correction below.

ChapterPageDetailsDatePrint Run
80 Error in Code
code lines:
if (csrfCookie.Value.Equals(tokenField))
      throw new Exception("Mismatched CSRF tokens");
should read:
if (!csrfCookie.Value.Equals(tokenField))
      throw new Exception("Mismatched CSRF tokens");
6 128 Typo in code
Third line in both pieces of code on this page: // Create an instance of our encyrption algorithm. should read: // Create an instance of our encryption algorithm.
15 February 2010
6 133 Typo in figure caption
Caption to Figure 6-2: Key use in symmetric encryption should read: Key use in asymmetric encryption
15 February 2010
197 Error in Text
The section “Adding a user to a database” should read:

Just because a login exists and can connect to SQL Server it doesn’t gain access to any databases. You must first grant an account access to the database. You can do this with the following SQL command:

USE [exampleDatabase]

This command creates a user within the database it is run in, in this example you first switch to the database exampleDatabase and then create a user Olle within for the SQL login account Olle. The user you create in a database does not have to have a name that matches with the actual login. If you want to create a user for a Windows login already granted access to SQL then you use the full Windows login details in the command, for example:

CREATE USER NetworkService FOR LOGIN [Puck\Network Service];

This command creates a user NetworkService for the Network Service account on the machine Puck, assuming you have already granted that Windows account access to the SQL server as described previously in “Connecting without Passwords”. You can use square brackets, [ and ] to enclose user names or account names if they contain spaces.

However adding a user to a database is only the first step, these new user accounts cannot do anything without some further work.
199 Error in Text
... remove permissions from everyone else, as shown here:
DENY SELECT ON employee TO Public

should be:

As you can imagine, salary is sensitive data, and you would not want to allow anyone who has not been authorized to view this data. If you cannot use stored procedures, you can use views to limit access. First, you remove permissions on the table itself from everyone in the Public role using the following command:

DENY SELECT ON employee TO Public

Then you specifically grant table permissions to those who are allowed access (the Accounting role, for example, for ad-hoc reporting) using the following command:

GRANT SELECT ON employee TO Accounting
10 251 Change in Code
Change in Code Listing 10-11:

public static bool VerifySignature(XmlDocument document, out X509Certificate signingCertificate)
            // Create a new SignedXml object and load
            // the signed XML document.
            SignedXml signedXml = new SignedXml(document);

            // Find the "Signature" node and create a new
            // XmlNodeList object.
            XmlNodeList nodeList = document.GetElementsByTagName("Signature");
            if (nodeList.Count <= 0)
                throw new CryptographicException("No signature found.");

            // Load the first  node.

            signingCertificate = null;

            // Extract the signing cerificate.
            foreach (KeyInfoClause keyInfoClause in signedXml.KeyInfo)
                if (!(keyInfoClause is KeyInfoX509Data))

                KeyInfoX509Data keyInfoX509Data = keyInfoClause as KeyInfoX509Data;
                if ((keyInfoX509Data.Certificates != null) && (keyInfoX509Data.Certificates.Count == 1))
                    signingCertificate = (X509Certificate)keyInfoX509Data.Certificates[0];
            // Check the signature.
            return signedXml.CheckSignature();
28 January 2010
Index 412 Typo in Index
Index entry refers to: XMLTs (XML Transformations), 234 should read: XSLTs (XML Transformations), 234
15 February 2010
See More
Buy Both and Save 25%!

Beginning ASP.NET Security (US $44.99)

-and- Teach Yourself VISUALLY Adobe Muse (US $29.99)

Total List Price: US $74.98
Discounted Price: US $56.23 (Save: US $18.75)

Buy Both
Cannot be combined with any other offers. Learn more.
Back to Top