Table Of Contentwww.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page i
Expert ASP.NET 2.0
Advanced Application Design
Dominic Selly
Andrew Troelsen
Tom Barnaby
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page ii
Expert ASP.NET 2.0 Advanced Application Design
Copyright © 2006 by Dominic Selly,Andrew Troelsen,and Tom Barnaby
Lead Editor: Ewan Buckingham
Technical Reviewers: Robert Lair, Rakesh Rajan, Victor Garcia Aprea, Fernando Simonazzi,
Hernan De Lahitte
Contributors: Dave Zimmerman, David Schueck, George Politis
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis,
JasonGilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Associate Publisher: Grace Wong
Project Manager: Beckie Stones
Copy Edit Manager: Nicole LeClerc
Copy Editor: Lauren Kennedy
Assistant Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor: Dina Quan
Proofreader: Nancy Sixsmith
Indexer: Broccoli Information Management
Artist: Wordstop
Interior Designer: Van Winkle Design Group
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi-
rectly by the information contained in this work.
The source code for this book is available to readers at http://www.apress.comin the Source Code section.
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page iii
Patty made Lucy
while I made this book,and so
I give it to them.
—dws
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page iv
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page v
Contents at a Glance
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
PART 1 ■ ■ ■ ASP.NET 2.0 Internals
■ CHAPTER 1 Overview of .NET Application Architecture . . . . . . . . . . . . . . . . . . . . . . 3
■ CHAPTER 2 The ASP.NET Request Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
■ CHAPTER 3 Page Object Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
■ CHAPTER 4 ViewState and Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
PART 2 ■ ■ ■ .NET Middle Tier Solutions
■ CHAPTER 5 .NET 2.0 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
■ CHAPTER 6 SOA in .NET:Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
■ CHAPTER 7 Enterprise Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
■ CHAPTER 8 Hosting and Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
■ CHAPTER 9 Windows Communication Foundation . . . . . . . . . . . . . . . . . . . . . . . . . 297
PART 3 ■ ■ ■ Data Access Layer
■ CHAPTER 10 Managed Providers of Data Access . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
■ CHAPTER 11 Data Access Layer Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
■ CHAPTER 12 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
v
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page vi
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page vii
Contents
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
PART 1 ■ ■ ■ ASP.NET 2.0 Internals
■ CHAPTER 1 Overview of .NET Application Architecture . . . . . . . . . . . . . . . . 3
Nonfunctional Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Scalability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Vertical Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Horizontal Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Maintainability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Other Nonfunctional Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Service Oriented Architecture/Service Orientation . . . . . . . . . . . . . . . . . . . 15
.NET Application Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
A Simple Managed Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using Serviced Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
A Widely Distributed Service Application . . . . . . . . . . . . . . . . . . . . . . 23
An Asynchronous Messaging Architecture . . . . . . . . . . . . . . . . . . . . . 26
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
■ CHAPTER 2 The ASP.NET Request Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Application and Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
The HttpApplication Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Built-in Framework Extensions to the Pipeline . . . . . . . . . . . . . . . . . 40
Extending the Request Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Inheriting from HttpApplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Implementing an HttpModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
vii
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page viii
viii ■ CONTENTS
HTTP Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Handlers Built into the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Creating an Http Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
■ CHAPTER 3 Page Object Internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Structure of a Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Options for Building Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Code Generation of ASPX Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Code Generation of ASPX Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Compilation and Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Dynamic Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Precompilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Basics of the Control Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Control Tree Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Web Form Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Control Events vs.Page Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The Special Role of the PreRender Event . . . . . . . . . . . . . . . . . . . . . . 90
Resource Consumption when Data Binding . . . . . . . . . . . . . . . . . . . . . . . . 93
Capturing the Rendered Output Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
■ CHAPTER 4 ViewState and Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Role of ViewState in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
The ViewState Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
2.0 ViewState Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Replacing ViewState Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Post-Cache Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Page Support for Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Out-of-Band Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
PART 2 ■ ■ ■ .NET Middle Tier Solutions
■ CHAPTER 5 .NET 2.0 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
A Survey of Core Security Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
The Role of Strongly Named Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . 148
An Example of Round Tripping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Preventing Roundtrip Engineering via Strong Naming . . . . . . . . . . 150
www.it-ebooks.info
522xFM.qxd 8/31/05 3:54 PM Page ix
■ CONTENTS ix
Encryption Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Understanding Hash Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Hashing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Role-Based Security (RBS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Restricting Access Based On User Identity . . . . . . . . . . . . . . . . . . . . 157
Securing ASP.NET Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Understanding Configuration Inheritance . . . . . . . . . . . . . . . . . . . . . 159
Understanding ASP.NET Authentication Options . . . . . . . . . . . . . . . . . . . . 160
Windows-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Passport-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Forms-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Leveraging the Forms Authentication Model . . . . . . . . . . . . . . . . . . . . . . . 163
Details behind the <authentication> Element . . . . . . . . . . . . . . . . . . . . . 165
Details behind the <forms> Element . . . . . . . . . . . . . . . . . . . . . . . . 166
Cookieless Authentication Support under ASP.NET 2.0 . . . . . . . . . 167
Details behind the <authorization> Element . . . . . . . . . . . . . . . . . . . . . . . 168
Details behind the FormsAuthentication Type . . . . . . . . . . . . . . . . . . . . . . 169
Working with the <credentials> Element . . . . . . . . . . . . . . . . . . . . . 170
Forms Authentication Improvements under ASP.NET 2.0 . . . . . . . . . . . . 171
Understanding the Membership Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Specifying a Membership Provider for your Website . . . . . . . . . . . 173
Adding New Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
The MembershipUser Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Updating a User’s Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Deleting a User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Working with the Security Server Controls . . . . . . . . . . . . . . . . . . . . . . . . 178
The Login Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
The LoginStatus Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The LoginName Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
The PasswordRecovery Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
The ChangePassword Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
The CreateUserWizard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Understanding ASP.NET 2.0 Role Membership . . . . . . . . . . . . . . . . . . . . . 186
Enabling Role Membership Services . . . . . . . . . . . . . . . . . . . . . . . . . 187
Understanding the Roles Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Creating,Obtaining,and Deleting Roles . . . . . . . . . . . . . . . . . . . . . . 189
Assigning Users to Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Determining Role Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
ASP.NET Web Application Administration Tool . . . . . . . . . . . . . . . . . . . . . . 191
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
www.it-ebooks.info