Tải bản đầy đủ (.pdf) (61 trang)

Building Secure ASP.NET Applications phần 1 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.99 MB, 61 trang )

Building Secure ASP.NET Applications
Authentication, Authorization, and Secure Communication
Web Server
Database Server
IIS
SQL Server
Clients
ASP.NET
IIS
ASP.NET
Authentication, Authorization and Secure Communication
Enterprise
Services
(COM+)
Web
Services
IIS
ASP.NET
.NET
Remoting
Information in this document, including URL and other Internet Web site
references, is subject to change without notice. Unless otherwise noted, the
example companies, organizations, products, domain names, e-mail addresses,
logos, people, places and events depicted herein are fictitious, and no association
with any real company, organization, product, domain name, e-mail address, logo,
person, place or event is intended or should be inferred. Complying with all
applicable copyright laws is the responsibility of the user. Without limiting the
rights under copyright, no part of this document may be reproduced, stored in or
introduced into a retrieval system, or transmitted in any form or by any means
(electronic, mechanical, photocopying, recording, or otherwise), or for any purpose,
without the express written permission of Microsoft Corporation.


Microsoft, MS-DOS, Windows, Active Directory, C#, Visual Basic, Visual Studio, and
Win32 are either registered trademarks or trademarks of Microsoft Corporation
in the United States and/or other countries.
© 2002 Microsoft Corporation. All rights reserved.
Version 1.0
The names of actual companies and products mentioned herein may be the
trademarks of their respective owners.
Contents
About This Book xxi
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Applies To . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What This Guide is About . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Who Should Read This Guide? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
What You Must Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Feedback and Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
The Team That Brought You This Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Contributors and Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
At a Glance xxvii
Chapter 1 – Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 2 – Security Model for ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 3 – Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Chapter 4 – Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix
Chapter 5 – Intranet Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxx
Chapter 6 – Extranet Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii
Chapter 7 – Internet Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii
Chapter 8 – ASP.NET Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv
Chapter 9 – Enterprise Services Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv
Chapter 10 – Web Services Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Chapter 11 – .NET Remoting Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxviii
Chapter 12 – Data Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix

Chapter 13 – Troubleshooting Security Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl
Chapter 1
Introduction 1
The Connected Landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What Are the Goals of This Guide? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How You Should Read This Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Organization of the Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Part I, Security Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Part II, Application Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Part III, Securing the Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Part IV, Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Key Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Contentsiv
Chapter 2
Security Model for ASP.NET Applications 9
.NET Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Logical Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Physical Deployment Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Implementation Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
.NET Remoting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
ADO.NET and SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Internet Protocol Security (IPSec) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Secure Sockets Layer (SSL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Security Across the Tiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Gatekeepers and Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Identities and Principals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
WindowsPrincipal and WindowsIdentity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
GenericPrincipal and Associated Identity Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 24
ASP.NET and HttpContext.User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Remoting and Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Chapter 3
Authentication and Authorization 29
Designing an Authentication and Authorization Strategy . . . . . . . . . . . . . . . . . . . . . . . 30
Identify Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Choose an Authorization Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Choose the Identities Used for Resource Access . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Consider Identity Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Choose an Authentication Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Decide How to Flow Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Authorization Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Role Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Resource Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Resource Access Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Trusted Subsystem Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Impersonation / Delegation Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Choosing a Resource Access Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Flowing Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Application vs. Operating System Identity Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Impersonation and Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

Contents v
Role-Based Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
.NET Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Enterprise Services (COM+) Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
SQL Server User Defined Database Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
SQL Server Application Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
.NET Roles versus Enterprise Services (COM+) Roles . . . . . . . . . . . . . . . . . . . . . . . 45
Using .NET Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Choosing an Authentication Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Internet Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Intranet / Extranet Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Authentication Mechanism Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Chapter 4
Secure Communication 55
Know What to Secure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
SSL/TLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Using SSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
IPSec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Using IPSec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
RPC Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Using RPC Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Point to Point Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Browser to Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Web Server to Remote Application Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Application Server to Database Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Choosing Between IPSec and SSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Farming and Load Balancing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Chapter 5
Intranet Security 65
ASP.NET to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Q&A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Related Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Contentsvi
ASP.NET to Enterprise Services to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
ASP.NET to Web Services to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Q&A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
ASP.NET to Remoting to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Flowing the Original Caller to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
ASP.NET to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
ASP.NET to Enterprise Services to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Chapter 6
Extranet Security 103
Exposing a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Q&A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Exposing a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Scenario Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Contents vii
Chapter 7
Internet Security 119

ASP.NET to SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Related Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
ASP.NET to Remote Enterprise Services to SQL Server . . . . . . . . . . . . . . . . . . . . . . . 127
Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Secure the Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
The Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Security Configuration Steps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Related Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Chapter 8
ASP.NET Security 137
ASP.NET Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Authentication and Authorization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Available Authorization Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Windows Authentication with Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Windows Authentication without Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Windows Authentication Using a Fixed Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Passport Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Configuring Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Configure IIS Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Configure ASP.NET Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Secure Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Programming Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
An Authorization Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Creating a Custom IPrincipal class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Development Steps for Forms Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Forms Implementation Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Hosting Multiple Applications Using Forms Authentication . . . . . . . . . . . . . . . . . . 168
Cookieless Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Contentsviii
Passport Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Custom Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Process Identity for ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Use a Least Privileged Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Avoid Running as SYSTEM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Using the Default ASPNET Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Impersonation and Local Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Impersonation and Remote Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Impersonation and Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Accessing System Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Accessing the Event Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Accessing the Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Accessing COM Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Apartment Model Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Accessing Network Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Using the ASP.NET Process Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

Using a Serviced Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Using the Anonymous Internet User Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Using LogonUser and Impersonating a Specific Windows Identity . . . . . . . . . . . . . 182
Using the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Accessing Files on a UNC File Share . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Accessing Non-Windows Network Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Storing Secrets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Options for Storing Secrets in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Consider Storing Secrets in Files on Separate Logical Volumes . . . . . . . . . . . . . . . 186
Securing Session and View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Securing View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Securing Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Securing SQL Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Web Farm Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
DPAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Using Forms Authentication in a Web Farm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
The <machineKey> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Chapter 9
Enterprise Services Security 195
Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Gatekeepers and Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Use Server Applications for Increased Security . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Security for Server and Library Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Code Access Security Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Contents ix
Configuring Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Configuring a Server Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Configuring an ASP.NET Client Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Configuring Impersonation Levels for an Enterprise Services Application . . . . . . . . 208
Programming Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Programmatic Role-Based Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Identifying Callers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Choosing a Process Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Never Run as the Interactive User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Use a Least-Privileged Custom Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Accessing Network Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Using the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Using the Current Process Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Using a Specific Service Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Flowing the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Calling CoImpersonateClient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
RPC Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Building Serviced Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
DLL Locking Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
QueryInterface Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
DCOM and Firewalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Calling Serviced Components from ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Caller’s Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Use Windows Authentication and Impersonation Within
the Web-based Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Configure Authentication and Impersonation within Machine.config . . . . . . . . . . . . 218
Configuring Interface Proxies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Enterprise Services (COM+) Roles and .NET Roles . . . . . . . . . . . . . . . . . . . . . . . . 222

Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Chapter 10
Web Services Security 227
Web Service Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Platform/Transport Level (Point-to-Point) Security . . . . . . . . . . . . . . . . . . . . . . . . . 228
Application Level Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Message Level (End-to-End) Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Platform/Transport Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Contentsx
Authentication and Authorization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Windows Authentication with Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Windows Authentication without Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Windows Authentication Using a Fixed Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Configuring Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Configure IIS Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Configure ASP .NET Settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Secure Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Disable HTTP-GET, HTTP-POST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Passing Credentials for Authentication to Web Services . . . . . . . . . . . . . . . . . . . . . . 240
Specifying Client Credentials for Windows Authentication . . . . . . . . . . . . . . . . . . . 241
Calling Web Services from Non-Windows Clients . . . . . . . . . . . . . . . . . . . . . . . . . 243
Proxy Server Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Flowing the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Default Credentials with Kerberos Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Explicit Credentials with Basic or Forms Authentication . . . . . . . . . . . . . . . . . . . . 247
Trusted Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

Flowing the Caller’s Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Configuration Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Accessing System Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Accessing Network Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Accessing COM Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Using Client Certificates with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Authenticating Web Browser Clients with Certificates . . . . . . . . . . . . . . . . . . . . . . 254
Using the Trusted Subsystem Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Transport Level Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Message Level Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Chapter 11
.NET Remoting Security 261
.NET Remoting Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Remoting Sinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Anatomy of a Request When Hosting in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . 264
ASP.NET and the HTTP Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
.NET Remoting Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Hosting in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Hosting in a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Using File Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Authentication and Authorization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Contents xi
Accessing System Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Accessing Network Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

Passing Credentials for Authentication to Remote Objects . . . . . . . . . . . . . . . . . . . . 272
Specifying Client Credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Flowing the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Default Credentials with Kerberos Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Explicit Credentials with Basic or Forms Authentication . . . . . . . . . . . . . . . . . . . . 278
Trusted Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Flowing the Caller’s Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Choosing a Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Configuration Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Platform Level Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Choosing a Host Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Hosting in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Hosting in a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Hosting in a Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Remoting vs. Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Chapter 12
Data Access Security 293
Introducing Data Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
SQL Server Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Trusted Subsystem vs. Impersonation/Delegation . . . . . . . . . . . . . . . . . . . . . . . . 295
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
SQL Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Authenticating Against Non-SQL Server Databases. . . . . . . . . . . . . . . . . . . . . . . . 305
Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Using Multiple Database Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

The Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Choosing an Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Connecting with Least Privilege. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
The Database Trusts the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
The Database Trusts Different Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
The Database Trusts the Original Caller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Creating a Least Privilege Database Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Storing Database Connection Strings Securely. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
The Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using DPAPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using Web.config and Machine.config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Contentsxii
Using UDL Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Using Custom Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Using the Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Using the COM+ Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Authenticating Users Against a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Store One-way Password Hashes (with Salt). . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
SQL Injection Attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Anatomy of a SQL Script Injection Attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Protecting Pattern Matching Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Auditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Process Identity for SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Chapter 13
Troubleshooting Security Issues 331
Process for Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Searching for Implementation Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Troubleshooting Authentication Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

IIS Authentication Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Using Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Using Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Kerberos Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Troubleshooting Authorization Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Check Windows ACLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Check Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Check the <authorization> Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Enable Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Configuration Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Determining Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Determining Identity in a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Determining Identity in a Web service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Determining Identity in a Visual Basic 6 COM Object . . . . . . . . . . . . . . . . . . . . . . 340
.NET Remoting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
SSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
IPSec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Auditing and Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Windows Security Logs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
SQL Server Auditing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
IIS Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Contents xiii
Troubleshooting Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
File Monitor (FileMon.exe) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Fusion Log Viewer (Fuslogvw.exe) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
ISQL.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Windows Task Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346

Network Monitor (NetMon.exe) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Registry Monitor (regmon.exe) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
WFetch.exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Visual Studio .NET Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
WebServiceStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Windows 2000 Resource Kit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
How To:
Index 349
ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Authentication and Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Enterprise Services Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Web Services Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Remoting Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
How To:
Create a Custom Account to Run ASP.NET 351
ASP.NET Worker Process Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Impersonating Fixed Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
1. Create a New Local Account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
2. Assign Minimum Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
3. Assign NTFS Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
4. Configure ASP.NET to Run Using the New Account. . . . . . . . . . . . . . . . . . . . . . . . . 356
How To:
Use Forms Authentication with Active Directory 357
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
1. Create a Web Application with a Logon Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

2. Configure the Web Application for Forms Authentication . . . . . . . . . . . . . . . . . . . . 359
3. Develop LDAP Authentication Code to Look Up the User in Active Directory . . . . . . 360
4. Develop LDAP Group Retrieval Code to Look Up the User’s Group Membership . . . 361
5. Authenticate the User and Create a Forms Authentication Ticket. . . . . . . . . . . . . . 362
6. Implement an Authentication Request Handler to Construct
a GenericPrincipal Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
7. Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Contentsxiv
How To:
Use Forms Authentication with SQL Server 2000 367
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
1. Create a Web Application with a Logon Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
2. Configure the Web Application for Forms Authentication . . . . . . . . . . . . . . . . . . . . 369
3. Develop Functions to Generate a Hash and Salt value . . . . . . . . . . . . . . . . . . . . . 370
4. Create a User Account Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
5. Use ADO.NET to Store Account Details in the Database . . . . . . . . . . . . . . . . . . . . 372
6. Authenticate User Credentials Against the Database . . . . . . . . . . . . . . . . . . . . . . 373
7. Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
How To:
Create GenericPrincipal Objects with Forms Authentication 377
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
1. Create a Web Application with a Logon Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
2. Configure the Web Application for Forms Authentication . . . . . . . . . . . . . . . . . . . . 379
3. Generate an Authentication Ticket for Authenticated Users. . . . . . . . . . . . . . . . . . 379
4. Construct GenericPrincipal and FormsIdentity Objects . . . . . . . . . . . . . . . . . . . . . 382
5. Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

How To:
Implement Kerberos Delegation for Windows 2000 385
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
1. Confirm that the Client Account is Configured for Delegation . . . . . . . . . . . . . . . . 386
2. Confirm that the Server Process Account is Trusted for Delegation . . . . . . . . . . . . 386
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
How To:
Implement IPrincipal 389
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
1. Create a Simple Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
2. Configure the Web Application for Forms Authentication . . . . . . . . . . . . . . . . . . . . 391
3. Generate an Authentication Ticket for Authenticated Users. . . . . . . . . . . . . . . . . . 392
4. Create a Class that Implements and Extends IPrincipal . . . . . . . . . . . . . . . . . . . . 394
5. Create the CustomPrincipal Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
5. Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Contents xv
How To:
Create a DPAPI Library 399
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
1. Create a C# Class Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
2. Strong Name the Assembly (Optional) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
How To:
Use DPAPI (Machine Store) from ASP.NET 409

Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
1. Create an ASP.NET Client Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
2. Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
3. Modify the Web Application to Read an Encrypted Connection String
from Web.Config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
How To:
Use DPAPI (User Store) from ASP.NET with Enterprise Services 415
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Why Use Enterprise Services? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Why Use a Windows Service? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
1. Create a Serviced Component that Provides Encrypt and Decrypt Methods . . . . . . 418
2. Call the Managed DPAPI Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
3. Create a Dummy Class that will Launch the Serviced Component . . . . . . . . . . . . . 420
4. Create a Windows Account to Run the Enterprise Services Application and
Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
5. Configure, Strong Name, and Register the Serviced Component . . . . . . . . . . . . . . 421
6. Create a Windows Service Application that will Launch the Serviced
Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
7. Install and Start the Windows Service Application . . . . . . . . . . . . . . . . . . . . . . . . 424
8. Write a Web Application to Test the Encryption and Decryption Routines . . . . . . . . 424
9. Modify the Web Application to Read an Encrypted Connection String from
an Application Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Contentsxvi
How To:

Create an Encryption Library 429
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
1. Create a C# Class Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
2. Create a Console Test Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
How To:
Store an Encrypted Connection String in the Registry 439
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
1. Store the Encrypted Data in the Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
2. Create an ASP.NET Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
How To:
Use Role-based Security with Enterprise Services 445
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
1. Create a C# Class Library Application to Host the Serviced Component. . . . . . . . . 446
2. Create the Serviced Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
3. Configure the Serviced Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
4. Generate a Strong Name for the Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
5. Build the Assembly and Add it to the Global Assembly Cache . . . . . . . . . . . . . . . . 449
6. Manually Register the Serviced Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
7. Examine the Configured Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
8. Create a Test Client Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
How To:
Call a Web Service Using Client Certificates from ASP.NET 453
Why Use a Serviced Component? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

Why is a User Profile Required? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
1. Create a Simple Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
2. Configure the Web Service Virtual Directory to Require Client Certificates . . . . . . . 456
3. Create a Custom Account for Running the Serviced Component . . . . . . . . . . . . . . 457
4. Request a Client Certificate for the Custom Account . . . . . . . . . . . . . . . . . . . . . . 457
5. Test the Client Certificate Using a Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Contents xvii
6. Export the Client Certificate to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
7. Develop the Serviced Component Used to Call the Web Service . . . . . . . . . . . . . . 460
8. Configure and Install the Serviced Component . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
9. Develop a Web Application to Call the Serviced Component . . . . . . . . . . . . . . . . . 464
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
How To:
Call a Web Service Using SSL 467
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
1. Create a Simple Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
2. Configure the Web Service Virtual Directory to Require SSL . . . . . . . . . . . . . . . . . 468
3. Test the Web Service Using a Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
4. Install the Certificate Authority’s Certificate on the Client Computer . . . . . . . . . . . 470
5. Develop a Web Application to Call the Web Service . . . . . . . . . . . . . . . . . . . . . . . 471
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
How To:
Host a Remote Object in a Windows Service 473
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
1. Create the Remote Object Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

2. Create a Windows Service Host Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
3. Create a Windows Account to Run the Service . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
4. Install the Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
5. Create a Test Client Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
How To:
Set Up SSL on a Web Server 479
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
1. Generate a Certificate Request. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
2. Submit a Certificate Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
3. Issue the Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
4. Install the Certificate on the Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
5. Configure Resources to Require SSL Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Contentsxviii
How To:
Set Up Client Certificates 485
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
1. Create a Simple Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
2. Configure the Web Application to Require Client Certificates . . . . . . . . . . . . . . . . . 486
3. Request and Install a Client Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
4. Verify Client Certificate Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
How To:
Use IPSec to Provide Secure Communication Between Two Servers 489
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
1. Create an IP Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

2. Create Filter Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
3. Create Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
4. Export the IPSec Policy to the Remote Computer . . . . . . . . . . . . . . . . . . . . . . . . . 495
5. Assign Policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
6. Verify that it Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
How To:
Use SSL to Secure Communication with SQL Server 2000 499
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
1. Install a Server Authentication Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
2. Verify that the Certificate Has Been Installed . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
3. Install the Issuing CA’s Certificate on the Client . . . . . . . . . . . . . . . . . . . . . . . . . . 502
4. Force All Clients to Use SSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
5. Allow Clients to Determine Whether to Use SSL . . . . . . . . . . . . . . . . . . . . . . . . . . 503
6. Verify that Communication is Encrypted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Base Configuration 509
Configuration Stores and Tools 511
Contents xix
Reference Hub 517
Searching the Knowledge Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
.NET Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Active Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Key Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Roadmaps and Overviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Roadmaps and Overviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Knowledge Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
How Tos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Knowledge Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Roadmaps and Overviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
How Tos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
FAQs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
IIS (Internet Information Server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Roadmaps and Overviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
How Tos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Roadmaps and Overviews: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524

Roadmaps and Overviews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
How Tos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Seminars and WebCasts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Windows 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Hubs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Contentsxx
How Does It Work? 527
IIS and ASP.NET Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Application Isolation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
The ASP.NET ISAPI Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
IIS 6.0 and Windows .NET Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
ASP.NET Pipeline Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
The Anatomy of a Web Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Implementing a Custom HTTP Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Implementing a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
ASP.NET Identity Matrix 537
Cryptography and Certificates 541
Keys and Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
X.509 Digital Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Certificate Stores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Technical Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Cryptography in .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
.NET Web Application Security 549
Glossary 551
About This Book
Summary

This guide presents a practical, scenario driven approach to designing and building
secure ASP.NET applications for Windows 2000 and version 1.0 of the .NET Frame-
work. It focuses on the key elements of authentication, authorization and secure
communication within and across the tiers of distributed .NET Web applications.
Applies To
The information in this guide applies to:

.NET Framework Version 1.0 Service Pack 2

ASP.NET

Enterprise Services

Web Services

.NET Remoting

ADO.NET

Visual Studio .NET

SQL Server 2000 Service Pack 2

Windows 2000 Service Pack 3
Recommendations and sample code in the guide were built and tested using Visual
Studio .NET and validated on servers running Windows 2000 Advanced Server SP 3,
.NET Framework SP 2, and SQL Server 2000 SP 2.
What This Guide is About
This guide focuses on:


Authentication (to identify the clients of your application)

Authorization (to provide access controls for those clients)

Secure communication (to ensure that messages remain private and are not
altered by unauthorized parties)
Building Secure ASP.NET Applicationsxxii
Why authentication, authorization, and secure communication?
Security is a broad topic. Research has shown that early design of authentication
and authorization eliminates a high percentage of application vulnerabilities.
Secure communication is an integral part of securing your distributed application to
protect sensitive data, including credentials, passed to and from your application,
and between application tiers.
There are many technologies used to build .NET Web applications. To build effec-
tive application-level authentication and authorization strategies, you need to
understand how to fine-tune the various security features within each product and
technology area, and how to make them work together to provide an effective,
defense-in-depth security strategy. This guide will help you do just that.
Figure 1 summarizes the various technologies discussed throughout the guide.
Web Server
Database Server
IIS
SQL Server
Clients
IIS
Authentication
Anonymous
Basic
Digest
Integrated

Certificates
Authorization
NTFS Perms
IP Restrictions
ASP.NET
Authentication
Windows
Forms
Passport
None (Custom)
Authorization
URL Auth
File Auth
.NET Roles
Enterprise Services
Authentication
RPC
Authorization
COM+ Roles
NTFS Perms
SQL Server
Authentication
Windows
SQL Auth
Authorization
Logins
Object Perms
Dbase Roles
User Roles
App Roles

ASP.NET
IIS
ASP.NET
Secure Communication (SSL / IPSec)
Enterprise
Services
(COM+)
Web
Services
IIS
ASP.NET
.NET
Remoting
Figure 1
.NET Web Application Security
About This Book xxiii
Who Should Read This Guide?
If you are a middleware developer or architect, who plans to build, or is currently
building .NET Web applications using one or more of the following technologies,
you should read this guide.

ASP.NET

Web Services

Enterprise Services

Remoting

ADO.NET

What You Must Know
To most effectively use this guide to design and build secure .NET Web applica-
tions, you should already have some familiarity and experience with .NET develop-
ment techniques and technologies. You should be familiar with distributed
application architecture and if you have already implemented .NET Web applica-
tion solutions, you should know your own application architecture and deployment
pattern.
Feedback and Support
Questions? Comments? Suggestions? For feedback on this security guide, please
send e-mail to
The security guide is designed to help you build secure .NET distributed applica-
tions. The sample code and guidance is provided as-is. While this material has
undergone testing and is considered a robust set of procedures and recommenda-
tions, it is not supported like a traditional Microsoft product.
The Team That Brought You This Guide
This guide was produced by the following .NET development specialists:

J.D. Meier, Microsoft, Program Manager, Prescriptive Architecture Guidance
(PAG)

Alex Mackman, Content Master Ltd, Founding member and Principal
Technologist

Srinath Vasireddy, Microsoft, Developer Support Engineer, Product Support
and Services (PSS)

Michael Dunner, Microsoft, Developer Support Engineer, Product Support
and Services (PSS)
Building Secure ASP.NET Applicationsxxiv
Contributors and Reviewers

Many thanks to the following contributors and reviewers:

Thanks to external reviewers – Keith Brown (DevelopMentor) for review and
feedback on the ASP.NET chapter, Andy Eunson for providing scenarios on
middleware applications, John Langley (KANA Software) for bringing J2EE
and .NET perspectives to the table, Kurt Dillard and Christof Sprenger for
reviewing application scenarios and the authentication and authorization
process, J.K.Meadows and David Alberto for reviewing application scenarios
and individual chapters and Bernard Chen (Sapient) for reviewing the
authentication and authorization process

Product Group – Thanks to Manish Prabhu, Jesus Ruiz-Scougall, Jonathan
Hawkins and Doug Purdy from the .NET Remoting team; Keith Ballinger Yann
Christensen and Alexei Vopilov from the Web Services team; Laura Barsan from
the ASP.NET team; Greg Fee (.NET Roles / Principal permission checks), Greg
Singleton and Sebastian Lange (CAS); Tarik Soulami from the CLR team; Erik
Olson (extensive validation and recommendations on ASP.NET); Caesar Samsi
(for sharing in depth e-commerce Internet facing application scenarios), Riyaz
Pishori, Shannon Pahl and Ron Jacobs (Enterprise Services), Dave McPherson
(Windows security architecture and authorization strategies), Christopher Brown
(helping resolve cross product issues), John Banes (DPAPI), Joel Scambray, Girish
Chander (SQL Server security)

MCS / Field – William Zentmayer (Remote application tier scenarios with Enter-
prise Services), Shantanu Sarkar (validation of application architecture sce-
narios), Carl Nolan (Web services), Samuel Melendez and Jacquelyn Schmidt
(infrastructure and deployment scenarios), Steve Busby, Len Cardinal, Monica
DeZulueta, Paula Paul (Data Access and Web application security), Ed Draper,
Sean Finnegan (pushing Active Directory and Windows authentication with
technical depth and practical scenarios), David Alberto, Kenny Jones (for bring-

ing real world field issues to the table and helping to involve the field), Doug
Orange (real world Extranet authorization scenarios), Alexey Yeltsov (SQL
Injection), Martin Kohlleppel (Architecture review), Joel Yoker (firewalls and
IPSec)

Special thanks to Jay Nanduri (Microsoft.com) for reviewing and sharing real
world experiences, Ilia Fortunov (Senior Architect) for providing continuous and
diligent feedback and Aaron Margosis (MCS) for thoroughly reviewing several
chapters and making excellent suggestions at various stages of the project.
About This Book xxv

Special thanks to Product Support Services folks for contributing and review
various portions of the guide – Venkat Chilakala (Troubleshooting section), John
Allen and Jeremy Bostron (ASP.NET), Martin Petersen-Frey (simplifying and
helping to structure the Remoting and Web Service portions of the guide), Karl
Westerholm (SSL), Jayaprakasam Siddian Thirunavukkarasu (SQL Roles and
ADO), Wade Mascia (valuable feedback on Enterprise Services, COM threading,
ASP.NET and Web services), Ryan Kivett (IIS6 and ASP.NET), Sarath
Mallavarapu (Data Access), Jerry Bryant (bringing community issues to the
table) and Peter Kyte for resources

Thanks also, to Philip Teale, Ram Sunkara, Shaun Hayes, Eric Schmidt, Michael
Howard, Rich Benack, Carlos Lyons, Ted Kehl. Thanks to Peter Dampier, Mike
Sherrill and Devendra Tiwari from the Enterprise Alliance team for validating
our application scenarios. Thanks to Tavi Siochi (IT Audit) for feedback on SQL
Injection.

Finally, thanks to our colleagues on the patterns & practices team: Per Vonge
Nielsen, Andrew Mason, Edward Jezierski, Sandy Khaund, RoAnn Corbisier,
Tina Burden, Edward Lafferty, Peter M. Clift, John Munyan, Mohammad Al-Sabt,

Anandha Murukan and Chris Sfanos

×