30
Laurence Moroney Matthew MacDonald (Ed.) Pro ASP.NET 2.0 in VB 2005

Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Laurence MoroneyMatthew MacDonald (Ed.)

Pro ASP.NET 2.0in VB 2005

Page 2: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Pro ASP.NET 2.0 in VB 2005

Copyright © 2006 by Laurence Moroney, Matthew MacDonald (Ed.), K. Scott Allen, James Avery, RussBasiura, Mike Batongbacal, Marco Bellinaso, Matt Butler, Andreas Eide, Daniel Cazzulino, Michael Clark,Richard Conway, Robert Eisenberg, Brady Gaster, James Greenwood, Kevin Hoffman, Erik Johansson,Angelo Kastroulis, Dan Kent, Sitaraman Lakshminarayanan, Don Lee, Christopher Miller, Matt Milner,Jan Narkiewicz, Matt Odhner, Ryan O’Keefe, Andrew Reid, Matthew Reynolds, Enrico Sabbadin, Bill Sempf,Doug Seven, Srinivasa Sivkumar, Thiru Thangarathinam, Doug Thews

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 retrievalsystem, without the prior written permission of the copyright owner and the publisher.

ISBN-13 (pbk): 978-1-59059-563-3ISBN-10 (pbk): 1-59059-563-7

Library of Congress Cataloging-in-Publication data is available upon request.

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 occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.

Lead Editor: Tony DavisTechnical Reviewer: Andy OlsenEditorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,

Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim SumserProject Manager: Richard Dal PortoCopy Edit Manager: Nicole LeClercCopy Editor: Kim WimpsettAssistant Production Director: Kari Brooks-CoponyProduction Editor: Linda MarousekCompositor and Artist: Kinetic Publishing Services, LLCProofreaders: Linda Seifert and Nancy SixsmithIndexer: Broccoli Information ManagementCover Designer: Kurt KramesManufacturing Director: Tom Debolski

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 [email protected], orvisit http://www.springeronline.com.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.

The information in this book is distributed on an “as is” basis, without warranty. Although every precautionhas been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability toany person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectlyby the information contained in this work.

The source code for this book is available to readers at http://www.apress.com in the Source Code section.You will need to answer questions pertaining to this book in order to successfully download the code.

Page 3: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

I would like to dedicate this book to a few people without whom it never would have been possible.

To Yaacov and Philippe Cohen at Mainsoft,who are remarkable men creating an excellent working

environment with God at its center. I appreciate every minute,every conversation, and every e-mail I get from you guys.

Thanks!

To my wife, Rebecca, and children, Claudia and Christopher,for their eternal patience and support.

Thank you so much!

To the God of Abraham, Isaac, Jacob, and Jesus through whom all things are possible.

Page 4: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Contents at a Glance

About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

About the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii

PART 1 ■ ■ ■ Core Concepts■CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

■CHAPTER 2 Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

■CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

■CHAPTER 4 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

■CHAPTER 5 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

■CHAPTER 6 State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

PART 2 ■ ■ ■ Data Access■CHAPTER 7 ADO.NET Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

■CHAPTER 8 Data Components and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

■CHAPTER 9 Data Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

■CHAPTER 10 Rich Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

■CHAPTER 11 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

■CHAPTER 12 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

■CHAPTER 13 Files and Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

PART 3 ■ ■ ■ Building ASP.NET Websites■CHAPTER 14 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

■CHAPTER 15 Themes and Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

■CHAPTER 16 Website Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

■CHAPTER 17 Resources and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

■CHAPTER 18 Website Deployment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619

v

Page 5: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

PART 4 ■ ■ ■ Security■CHAPTER 19 The ASP.NET Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

■CHAPTER 20 Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

■CHAPTER 21 Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

■CHAPTER 22 Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

■CHAPTER 23 Authorization and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

■CHAPTER 24 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

■CHAPTER 25 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

■CHAPTER 26 Custom Membership Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867

PART 5 ■ ■ ■ Advanced User Interface■CHAPTER 27 Custom Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897

■CHAPTER 28 Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939

■CHAPTER 29 JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973

■CHAPTER 30 Dynamic Graphics and GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011

■CHAPTER 31 Portals with WebPart Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043

PART 6 ■ ■ ■ Web Services■CHAPTER 32 Creating Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087

■CHAPTER 33 Web Service Standards and Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . 1129

■CHAPTER 34 Advanced Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209

vi

Page 6: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Contents

About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

About the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxi

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii

PART 1 ■ ■ ■ Core Concepts■CHAPTER 1 Introducing ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

The Evolution of Web Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The Development World Before ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . 4What’s Wrong with Classic ASP? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4ASP.NET 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Seven Important Facts About ASP.NET. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Fact 1: ASP.NET Is Integrated with the .NET Framework . . . . . . . . . . . . . . . 7Fact 2: ASP.NET Is Compiled, Not Interpreted. . . . . . . . . . . . . . . . . . . . . . . . 7Fact 3: ASP.NET Is Multilanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Fact 4: ASP.NET Runs Inside the Common Language Runtime . . . . . . . . . 11Fact 5: ASP.NET Is Object-Oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Fact 6: ASP.NET Is Multidevice and Multibrowser . . . . . . . . . . . . . . . . . . . 14Fact 7: ASP.NET Is Easy to Deploy and Configure . . . . . . . . . . . . . . . . . . . 14

ASP.NET 2.0: The Story Continues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Visual Basic 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

■CHAPTER 2 Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

The .NET Development Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24The Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25The Visual Studio 2005 IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Websites in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Projectless Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Migrating a Visual Studio .NET Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Designing a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

vii

Page 7: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

The Visual Studio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Solution Explorer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Document Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Error List and Task List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Server Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

The Code Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Adding Assembly References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42IntelliSense and Outlining. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

The Coding Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47How Code-Behind Files Are Connected to Pages. . . . . . . . . . . . . . . . . . . . 50How Control Tags Are Connected to Page Variables . . . . . . . . . . . . . . . . . 50How Events Are Connected to Event Handlers . . . . . . . . . . . . . . . . . . . . . . 51

Visual Studio Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Single-Step Debugging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Advanced Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Variable Watches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

Visual Studio Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58ASP.NET Development Helper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

■CHAPTER 3 Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Page Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Dynamic Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66The ASP.NET Event Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66Automatic Postbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67View State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69XHTML Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

Web Forms Processing Stages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Page Framework Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77User Code Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Automatic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Cleanup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79A Page Flow Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

The Page As a Control Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Showing the Control Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82The Page Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Dynamic Control Creation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

■CONTENTSviii

Page 8: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

The Page Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Session, Application, and Cache. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Request. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Accessing the HTTP Context in Another Class . . . . . . . . . . . . . . . . . . . . . 101

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

■CHAPTER 4 Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Types of Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104The Server Control Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106The HtmlControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107The HtmlContainerControl Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108The HtmlInputControl Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108The HTML Server Control Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Setting Style Attributes and Other Properties. . . . . . . . . . . . . . . . . . . . . . 110Programmatically Creating Server Controls . . . . . . . . . . . . . . . . . . . . . . . 111Handling Server-Side Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116The WebControl Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Basic Web Control Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Enumerated Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Focus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122The Default Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Scrollable Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Handling Web Control Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

The List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127The Selectable List Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128The BulletedList Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

Input Validation Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132The Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132The Validation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133The BaseValidator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135The RequiredFieldValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135The RangeValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

■CONTENTS ix

Page 9: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

The CompareValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136The RegularExpressionValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . 137The CustomValidator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140The ValidationSummary Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Using the Validators Programmatically. . . . . . . . . . . . . . . . . . . . . . . . . . . 142Validation Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

The ASP.NET Rich Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144The AdRotator Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145The Calendar Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

■CHAPTER 5 ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

Anatomy of an ASP.NET Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151The Application Domain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Application Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Application Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Application Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

The Global.asax Application File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Demonstrating Application Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

ASP.NET Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159The Machine.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160The Web.config File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Configuration Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Reading and Writing Configuration Sections Programmatically . . . . . . . 169The Website Administration Tool (WAT) . . . . . . . . . . . . . . . . . . . . . . . . . . 171Extending the Configuration File Structure. . . . . . . . . . . . . . . . . . . . . . . . 173Encrypting Configuration Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

.NET Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Creating a Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Using a Component Through the App_Code Directory. . . . . . . . . . . . . . . 179Using a Component Through the Bin Directory . . . . . . . . . . . . . . . . . . . . 180

Extending the HTTP Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182HTTP Handlers and HTTP Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Creating a Custom HTTP Handler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Configuring a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Registering HTTP Handlers Without Configuring IIS. . . . . . . . . . . . . . . . . 187Creating an Advanced HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Creating a Custom HTTP Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

■CONTENTSx

Page 10: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 6 State Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

ASP.NET State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

A View State Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Storing Objects in View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Retaining Member Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202Assessing View State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Trimming View State in a List Control . . . . . . . . . . . . . . . . . . . . . . . . . . . 205View State Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Transferring Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207The Query String. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Cross-Page Posting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Cross-Page Posting and Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

Custom Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

Session Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214Using Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Configuring Session State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217Securing Session State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Shared Application Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

PART 2 ■ ■ ■ Data Access■CHAPTER 7 ADO.NET Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

The ADO.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230ADO.NET Data Providers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Standardization in ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233Fundamental ADO.NET Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

The Connection Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Connection Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Testing a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Connection Pooling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Connection Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240

The Command and DataReader Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Command Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240The DataReader Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

■CONTENTS xi

Page 11: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

The ExecuteReader() Method and the DataReader . . . . . . . . . . . . . . . . . 242The ExecuteScalar() Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247The ExecuteNonQuery() Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247SQL Injection Attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248Using Parameterized Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Transactions and ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 254Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Nested Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

Provider-Agnostic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Creating the Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Create Objects with Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261A Query with Provider-Agnostic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

■CHAPTER 8 Data Components and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . 265

Building a Data Access Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265The Data Package. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267The Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268The Data Utility Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Testing the Component. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Disconnected Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Web Applications and the DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276XML Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

The DataSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277The DataTable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279The DataRow Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

The DataAdapter Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Filling a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Working with Multiple Tables and Relationships . . . . . . . . . . . . . . . . . . . 282Searching for Specific Rows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Using the DataSet in a Custom Data Class . . . . . . . . . . . . . . . . . . . . . . . 286Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

The DataView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Sorting with a DataView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Filtering with a DataView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Advanced Filtering with Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . 291Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

■CONTENTSxii

Page 12: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 9 Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Basic Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Single-Value Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Other Types of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Repeated-Value Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309The Page Life Cycle with Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . 310

The SqlDataSource. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311Selecting Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312Parameterized Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Disadvantages of the SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

The ObjectDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323Selecting Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328Updating with a Data Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

The Limits of the Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Adding the Extra Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333Handling the Extra Options with the SqlDataSource . . . . . . . . . . . . . . . . 333Handling the Extra Options with the ObjectDataSource. . . . . . . . . . . . . . 334

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

■CHAPTER 10 Rich Data Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

The GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Defining Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Formatting the GridView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Formatting Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341Formatting-Specific Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344

GridView Row Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Using Selection to Create a Master-Details Form . . . . . . . . . . . . . . . . . . 347The SelectedIndexChanged Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349Using a Data Field As a Select Button . . . . . . . . . . . . . . . . . . . . . . . . . . . 350

Sorting the GridView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Sorting with the SqlDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351Sorting with the ObjectDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352Sorting and Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353Advanced Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

■CONTENTS xiii

Page 13: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Paging the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355Automatic Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356Custom Pagination with the ObjectDataSource . . . . . . . . . . . . . . . . . . . . 357Customizing the Pager Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

GridView Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Using Multiple Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Editing Templates in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Binding to a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364Handling Events in a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Editing with a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

The DetailsView and FormView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372The DetailsView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372The FormView. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

Advanced Grids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Summaries in the GridView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376A Parent/Child View in a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378Serving Images from a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380Detecting Concurrency Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

■CHAPTER 11 Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Understanding ASP.NET Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392Output Caching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Declarative Output Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Caching and the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394Caching with Specific Query String Parameters . . . . . . . . . . . . . . . . . . . 395Custom Caching Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396Caching with the HttpCachePolicy Class . . . . . . . . . . . . . . . . . . . . . . . . . 397Post-Cache Substitution and Fragment Caching . . . . . . . . . . . . . . . . . . . 398Cache Profiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400Caching to Disk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

Data Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Adding Items to the Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402A Simple Cache Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404Cache Priorities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405Caching with the Data Source Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 406

Cache Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409File and Cache Item Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409Aggregate Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411The Item Removed Callback. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411Understanding SQL Cache Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . 413Cache Notifications in SQL Server 2000 or SQL Server 7 . . . . . . . . . . . . 415Cache Notifications in SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . 419

■CONTENTSxiv

Page 14: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Custom Cache Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420A Basic Custom Cache Dependency . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421A Custom Cache Dependency Using Message Queues . . . . . . . . . . . . . . 422

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

■CHAPTER 12 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

When Does Using XML Make Sense? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426An Introduction to XML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

The Advantages of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Well-Formed XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428XML Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428XML Schemas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

Writing and Reading XML Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Writing XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Reading XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434Searching an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440Validating XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446

Displaying XML Content with XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448A Basic Stylesheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449Using XslCompiledTransform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450Using the Xml Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

XML Data Binding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Nonhierarchical Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451Using XPath Binding Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453Nested Grids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455Hierarchical Binding with the TreeView . . . . . . . . . . . . . . . . . . . . . . . . . . 457Using XSLT to Prepare Data for Binding in a TreeView. . . . . . . . . . . . . . . 460Binding to XML Content from Other Sources . . . . . . . . . . . . . . . . . . . . . . 461Updating XML Through the XmlDataSource. . . . . . . . . . . . . . . . . . . . . . . 462

XML and ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462Converting the DataSet to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463Accessing a DataSet As XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464Executing an XML Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468

■CHAPTER 13 Files and Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

Working with the File System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469The Directory and File Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470The DirectoryInfo and FileInfo Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 472The DriveInfo Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474Working with Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475Filter Files with Wildcards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

■CONTENTS xv

Page 15: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Retrieving File Version Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477The Path Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478A File Browser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480

Reading and Writing Files with Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488Uploading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489Making Files Safe for Multiple Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490Compression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494

Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

PART 3 ■ ■ ■ Building ASP.NET Websites■CHAPTER 14 User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

User Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502Creating a Simple User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502Converting a Page to a User Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

Adding Code to a User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504Handling Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504Adding Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506Using Custom Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Adding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510Exposing the Inner Web Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

Dynamically Loading User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514Portal Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

Partial Page Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518VaryByControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519Sharing Cached Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

■CHAPTER 15 Themes and Master Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

Standardizing Website Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526Theme Folders and Skins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527Applying a Simple Theme. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528Handling Theme Conflicts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529Creating Multiple Skins for the Same Control . . . . . . . . . . . . . . . . . . . . . 530Skins with Templates and Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531Using CSS in a Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533

■CONTENTSxvi

Page 16: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Applying Themes Through a Configuration File . . . . . . . . . . . . . . . . . . . . 534Applying Themes Dynamically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

Standardizing Website Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536Master Page Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

A Simple Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537A Simple Content Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539Design-Time Quirks with Master Pages. . . . . . . . . . . . . . . . . . . . . . . . . . 541Default Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543A More Practical Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544Master Pages and Relative Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545Master Pages and Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546Applying Master Pages Through a Configuration File . . . . . . . . . . . . . . . 547

Advanced Master Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547Specifying a Title and Metatags for a Content Page . . . . . . . . . . . . . . . . 547Interacting with the Master Page Class . . . . . . . . . . . . . . . . . . . . . . . . . . 548Dynamically Setting a Master Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550Nesting Master Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552

■CHAPTER 16 Website Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553

Pages with Multiple Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553The MultiView Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554The Wizard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558

Site Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565Defining a Site Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566Binding to a Site Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567Breadcrumbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569Binding Portions of a SiteMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571Programmatic Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574Binding Other Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575Adding Custom Site Map Information . . . . . . . . . . . . . . . . . . . . . . . . . . . 576Creating a Custom SiteMapProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577URL Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581

The TreeView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582The TreeNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583Populating Nodes on Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585TreeView Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587

Menu Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591Menu Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594Menu Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597

■CONTENTS xvii

Page 17: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 17 Resources and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599

Resources in .NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599Localization of Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607

Localization and the Common Language Runtime . . . . . . . . . . . . . . . . . 607Local Resources for a Single Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610Sharing Resources Between Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615Localizing Static Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617Text Directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618

■CHAPTER 18 Website Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619

Internet Information Services (IIS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619IIS and URL Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620Request Processing with IIS and ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . 622IIS 5.x Process Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623IIS 6.0 Process Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627Installing IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632

Managing Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635Creating a Virtual Directory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636Virtual Directories and Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . 638Folder Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638

Managing Application Pools in IIS 6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 642Creating Application Pools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643Application Pools and Web Applications. . . . . . . . . . . . . . . . . . . . . . . . . . 645Custom Application Pool Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646

Deploying Your ASP.NET Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649Verifying the ASP.NET Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650ASP.NET Side-By-Side Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651Configure HTTP Runtime Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653Compilation Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 654Deploying with Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655

The VirtualPathProvider in ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657Health Monitoring in ASP.NET 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662

Understanding the Basic Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662Events and Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666

■CONTENTSxviii

Page 18: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

PART 4 ■ ■ ■ Security■CHAPTER 19 The ASP.NET Security Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669

What It Means to Create Secure Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669Understanding Potential Threats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670Secure Coding Guidelines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 670Understanding Gatekeepers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671

Understanding the Levels of Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Confidentiality and Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674Pulling It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675

Internet Information Services Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676IIS Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677IIS Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678IIS and Secure Sockets Layer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679

ASP.NET Security Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687The Security Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 688Membership and Roles APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 690

■CHAPTER 20 Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691

Introducing Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691Why Use Forms Authentication?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692Why Would You Not Use Forms Authentication?. . . . . . . . . . . . . . . . . . . . 693Why Not Implement Cookie Authentication Yourself? . . . . . . . . . . . . . . . 695The Forms Authentication Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696

Implementing Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 696Configuring Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697Denying Access to Anonymous Users . . . . . . . . . . . . . . . . . . . . . . . . . . . 699Creating a Custom Login Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700Custom Credentials Store. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705Persistent Cookies in Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . 706

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708

■CONTENTS xix

Page 19: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 21 Membership. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709

Introducing the ASP.NET Membership API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709Using the Membership API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711

Configuring Forms Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713Creating the Data Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714Configuring Connection String and Membership Provider . . . . . . . . . . . . 718Creating and Authenticating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721

Using the Security Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723The Login Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724The LoginStatus Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732The LoginView Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733The PasswordRecovery Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734The ChangePassword Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739The CreateUserWizard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740

Using the Membership Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744Retrieving Users from the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745Updating Users in the Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747Creating and Deleting Users. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748Validating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749

■CHAPTER 22 Windows Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751

Introducing Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751Why Use Windows Authentication? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752Why Would You Not Use Windows Authentication? . . . . . . . . . . . . . . . . . 753Mechanisms for Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . 753

Implementing Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760Configuring IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760Configuring ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762Denying Access to Anonymous Users . . . . . . . . . . . . . . . . . . . . . . . . . . . 762Accessing Windows User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . 763

Impersonation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Impersonation in Windows 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Impersonation on Windows XP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768Impersonation and Delegation on Windows Server 2003 . . . . . . . . . . . . 768Configured Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771Programmatic Impersonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 776

■CONTENTSxx

Page 20: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 23 Authorization and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777

URL Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777Authorization Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778

File Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783Authorization Checks in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784

Using the IsInRole() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784Using the PrincipalPermission Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784

Using the Roles Service for Role-Based Authorization . . . . . . . . . . . . . . . . . . . 786Using the LoginView Control with Roles. . . . . . . . . . . . . . . . . . . . . . . . . . 792Accessing Roles Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793Using the Roles Service with Windows Authentication . . . . . . . . . . . . . . 795

Protecting Non–Web Page Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797Adding a File Type Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798Writing a Custom HTTP Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801

■CHAPTER 24 Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

Understanding Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803Profile Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803How Profiles Store Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804Profiles and Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805Profiles vs. Custom Data Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 805

Using the SqlProfileProvider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806Creating the Profile Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806Configuring the Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809Defining Profile Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809Using Profile Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 810Profile Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812Profile Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814Profiles and Custom Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814The Profiles API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818Anonymous Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820

Building a Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822The Shopping Cart Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823The Test Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825Multiple Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828

Custom Profiles Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828The Custom Profiles Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 828Designing the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . 830Coding the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831Testing the FactoredProfileProvider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 837

■CONTENTS xxi

Page 21: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■CHAPTER 25 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839

Encrypting Data: Confidentiality Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839The .NET Cryptography Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Understanding the .NET Cryptography Classes . . . . . . . . . . . . . . . . . . . . . . . . . 843

Symmetric Encryption Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844Asymmetric Encryption. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845The Abstract Encryption Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846The ICryptoTransform Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846The CryptoStream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 847

Encrypting Sensitive Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848Managing Secrets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848Using Symmetric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 850Using Asymmetric Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 855Encrypting Sensitive Data in a Database . . . . . . . . . . . . . . . . . . . . . . . . . 858

Encrypting the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861Wrapping the Query String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862Creating a Test Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866

■CHAPTER 26 Custom Membership Providers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867

Architecture of Custom Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867Basic Steps for Creating Custom Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869

Overall Design of the Custom Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . 869Designing and Implementing the Custom Store. . . . . . . . . . . . . . . . . . . . 870Implementing the Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876Using the Custom Provider Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 892

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894

PART 5 ■ ■ ■ Advanced User Interface■CHAPTER 27 Custom Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897

Custom Server Control Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 898Creating a Bare-Bones Custom Control . . . . . . . . . . . . . . . . . . . . . . . . . . 898Using a Custom Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 900Custom Controls in the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 901Creating a WebControl That Supports Style Properties . . . . . . . . . . . . . . 903The Rendering Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906

Dealing with Different Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908The HtmlTextWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908Browser Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909Browser Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 910Adaptive Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912

■CONTENTSxxii

Page 22: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Control State and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913View State. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913Control State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915Postback Data and Change Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917Triggering a Postback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919

Extending Existing Web Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921Composite Controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921Derived Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924

Templated Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929Creating a Templated Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929Using Customized Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 932Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 938

■CHAPTER 28 Design-Time Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939

Design-Time Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940The Properties Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 940Attributes and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944The Toolbox Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944

Web Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946Code Serialization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948

Type Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948Serialization Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955Type Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961

Control Designers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964A Basic Control Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964

Smart Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967The Action List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 967The DesignerActionItem Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969The Control Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971

■CHAPTER 29 JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973

JavaScript Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973JavaScript Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974Script Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976Rendering Script Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 983

Script Injection Attacks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985Request Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985Disabling Request Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987

■CONTENTS xxiii

Page 23: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Client Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988Creating a Client Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989Client Callbacks “Under the Hood”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993

Custom Controls with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994Pop-Up Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 995Rollover Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998Dynamic Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001

Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005Frame Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006Inline Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009

■CHAPTER 30 Dynamic Graphics and GDI+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011

The ImageMap Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1011Creating Hotspots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012Handling Hotspot Clicks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013A Custom Hotspot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014

Drawing with GDI+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016Simple Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017Image Format and Quality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019The Graphics Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020Using a GraphicsPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022Pens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024Brushes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026

Embedding Dynamic Graphics in a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . 1028Using the PNG Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029Passing Information to Dynamic Images . . . . . . . . . . . . . . . . . . . . . . . . 1030Custom Controls That Use GDI+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033

Charting with GDI+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1042

■CHAPTER 31 Portals with WebPart Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043

Typical Portal Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1043Basic WebPart Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045

Creating the Page Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1046WebPartManager and WebPartZones . . . . . . . . . . . . . . . . . . . . . . . . . . . 1047Adding WebParts to the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049Customizing the Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052

Creating WebParts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054Simple WebPart Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1054Developing Advanced WebParts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061WebPart Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1069

■CONTENTSxxiv

Page 24: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Connecting WebParts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075Authorizing WebParts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081Final Tasks for Personalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082Clearing Personalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1082

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083

PART 6 ■ ■ ■ Web Services■CHAPTER 32 Creating Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1087

Web Services Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1088The History of Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1089Distributed Computing and Web Services . . . . . . . . . . . . . . . . . . . . . . . 1090The Problems with Distributed Component Technologies . . . . . . . . . . . 1091The Benefits of Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1091Making Money with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093The Web Service Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1093

Building a Basic Web Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096The Web Service Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096Web Service Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097Exposing a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100Testing a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1103

Consuming a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1106The Proxy Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112Creating an ASP.NET Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1113Creating a Windows Forms Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1115Creating an ASP Client with MSXML . . . . . . . . . . . . . . . . . . . . . . . . . . . 1117Creating an ASP Client with the SOAP Toolkit . . . . . . . . . . . . . . . . . . . . 1119

Refining a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120CacheDuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121EnableSession. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123BufferResponse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126TransactionOption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1126

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1128

■CHAPTER 33 Web Service Standards and Extensions . . . . . . . . . . . . . . . . . . . 1129

WS-Interoperability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1129SOAP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1131

SOAP Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1132SOAP Versions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1133Tracing SOAP Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1134The SOAP Envelope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136The SOAP Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1140

■CONTENTS xxv

Page 25: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1144Viewing the WSDL for a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . 1144The Basic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146Implementing an Existing Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1150

Customizing SOAP Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152Serializing Complex Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152Customizing XML Serialization with Attributes. . . . . . . . . . . . . . . . . . . . 1157Customizing XML Serialization with IXmlSerializable . . . . . . . . . . . . . . 1160Custom Serialization for Large Data Types . . . . . . . . . . . . . . . . . . . . . . 1163Schema Importer Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1169

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1171

■CHAPTER 34 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173

Asynchronous Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173Asynchronous Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1174A Simple Asynchronous Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175Concurrent Asynchronous Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178Responsive Windows Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1179Asynchronous Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182

Securing Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1183Windows Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1184Custom Ticket-Based Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186Tracking the User Identity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187Authenticating the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188Authorizing the User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1188Testing the SOAP Authentication System . . . . . . . . . . . . . . . . . . . . . . . . 1189

SOAP Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191Creating a SOAP Extension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1193

The Web Services Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1200Installing the WSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1201Performing Authentication with the WSE . . . . . . . . . . . . . . . . . . . . . . . . 1203

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1208

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209

■CONTENTSxxvi

Page 26: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

■LAURENCE MORONEY is the director of technology evangelism at Mainsoft,where he works to promote and shape the company’s innovative cross-platform strategy, extending the ASP.NET framework to the J2EE platform.Author of Pro ASP.NET 1.1 in VB .NET (Apress, 2005) and the forthcomingFoundations of Atlas (Apress, 2006), as well as coauthor of J2EE .NETInteroperability: Integration Strategies (Prentice-Hall, 2006) and Expert WebServices Security in the .NET Platform (Apress, 2004), he is usually foundhunched down over his laptop typing something with a distant look on hisface. He is definitely not a running back for Minnesota and also not in con-tention for the Heisman Trophy, but if they want to give it to him, he wouldbe happy to oblige.

xxvii

About the Author

Page 27: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

About the Editor

■MATTHEW MACDONALD is an author, educator, and MCSD developer. He isa regular contributor to programming journals and the author/coauthor ofmore than a dozen books about .NET programming, including ASP.NET:The Complete Reference (Osborne McGraw-Hill, 2002), Programming .NETWeb Services (O’Reilly, 2002), Beginning ASP.NET 1.1 in C# (Apress, 2004),Pro ASP.NET 2.0 in C# 2005 (Apress, 2005), and Microsoft .NET DistributedApplications (Microsoft Press, 2003). In a dimly remembered past life, hestudied English literature and theoretical physics.

xxix

Page 28: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

About the Technical Reviewer

■ANDY OLSEN is a freelance consultant and developer based in the United Kingdom. Andy has beenworking with Microsoft technologies since around 1990 but considers it much more fun thesedays! He is a regular speaker at conferences in the United Kingdom, Europe, and the United States,and he has also written several courses and white papers for Microsoft. You can contact him [email protected] if you want to discuss .NET, football, or rugby.

xxxi

Page 29: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

Introduction

It’s not hard to get developers interested in ASP.NET. Without exaggeration, ASP.NET is the mostcomplete platform for web development that’s ever been put together. It far outclasses its predecessor,ASP, which was designed as a quick-and-dirty set of tools for inserting dynamic content into ordinaryweb pages. By contrast, ASP.NET is a full-blown platform for developing comprehensive, blisteringlyfast web applications.

In this book, you’ll learn everything you need to master ASP.NET 2.0. If you’ve programmed witha previous version of ASP.NET, you’ll sail through the basics and quickly begin learning about theexciting new features in version 2.0. If you’ve never programmed with ASP.NET, you’ll find that thisbook provides a well-paced tour that leads you through all the fundamentals and gives you a back-stage pass so you can see how the ASP.NET internals really work.

ASP.NET from 1.0 to 2.0As you no doubt already know, ASP.NET is Microsoft’s next-generation technology for creatingserver-side web applications. It’s built on the Microsoft .NET Framework, which is a cluster of closelyrelated new technologies that revolutionize everything from database access to distributed applica-tions. ASP.NET is one of the most important components of the .NET Framework—it’s the part thatenables you to develop high-performance web applications and web services.

ASP.NET 1.0 was a revolution in the web programming world. It was so wildly popular that it waslicensed on thousands of commercial web servers through Microsoft’s Go-Live license program whileit was still a beta product. ASP.NET 1.0 was finally released in early 2002.

ASP.NET 1.1 wasn’t as ambitious. Instead, it was just a chance for Microsoft architects to pauseand catch their collective breath. The focus in ASP.NET 1.1 wasn’t on new features—there weren’t any—but on performance tune-ups, security tweaks, and minor bug fixes. New features were quietly shelvedand saved for the next major milestone, ASP.NET 2.0. ASP.NET 1.1 was released late in 2003, solidify-ing ASP.NET as the web development platform of choice for professional developers.

Two long years later, ASP.NET 2.0 finally appeared on the horizon. Unlike the ASP.NET 1.0 release,ASP.NET 2.0 doesn’t represent the start of a new direction in web development. In fact, almost all theunderlying architecture that underpins ASP.NET 1.0 remains the same in ASP.NET 2.0. The differenceis that ASP.NET 2.0 adds layers of higher-level features to the existing technology. Essentially, after thesuccess of ASP.NET 1.0, Microsoft poured developers, time, and resources into planning and preparingASP.NET 2.0. Seeing as they no longer needed to rewrite the ASP.NET engine, the ASP.NET teammembers were free to innovate new controls, create better data management solutions, builda role-based security framework, and even make a whole toolkit for creating portal websites. In short,ASP.NET 2.0 gives developers a chance to relax and enjoy a wealth of new frills designed for theirfavorite platform. In this book, you’ll learn how to use, customize, and extend all these features.

xxxiii

Page 30: Pro ASP.NET 2.0 in VB 2005€¦ · I would like to dedicate this book to a few people without whom it never would have been possible. To Yaacov and Philippe Cohen at Mainsoft, who

What Does This Book Cover?Here is a quick breakdown of what you’ll find in this book:

Part 1: Core Concepts: You’ll begin in Chapter 1 with a look at the overall ASP.NET platform, the.NET Framework, and the changes in store for ASP.NET 2.0. In Chapter 2 you’ll branch out to learnthe tools of the trade—namely, Visual Studio 2005. In Chapters 3, 4, 5, and 6 you’ll learn the keyparts of the ASP.NET infrastructure, such as the web-page model, application configuration, statemanagement, and caching. As you learn these core concepts, you’ll also take a low-level look athow ASP.NET processes requests and manages the lifetime of your web applications. You’ll evenlearn how to extend the ASP.NET architecture.

Part 2: Data Access: This part tackles one of the core problem domains for all software develop-ment—accessing and manipulating data. In Chapters 7 and 8 you’ll consider the fundamentalsof ADO.NET as they apply to web applications and learn how to design data access components.In Chapter 9 and Chapter 10 you’ll learn about ASP.NET’s set of innovative data-bound controlsthat let you format and present data without writing pages of code. Chapter 11 branches outinto advanced caching strategies that ensure blistering performance. Finally, Chapters 12and 13 move beyond the world of databases to show you how to work with XML content andhandle ordinary file access.

Part 3: Building ASP.NET Websites: In this part you’ll learn about essential techniques and fea-tures for managing groups of web pages. You’ll start simply with user controls in Chapter 14, whichallow you to reuse segments of the user interface. In Chapter 15 you’ll consider two new ASP.NETinnovations—themes (for styling controls automatically) and master pages (for reusing a layouttemplate across multiple pages). Chapter 16 shows how you can use the new navigation model inASP.NET 2.0 to let visitors surf from one page to another. Finally, Chapter 17 explores localization,and Chapter 18 describes deployment and the IIS web server software.

Part 4: Security: In this part you’ll look at ASP.NET’s rich complement of security features. You’llstart with a high-level overview of security concepts in Chapter 19 and then learn the ins and outs offorms authentication (Chapter 20) and the new membership API that works with it (Chapter 21). InChapter 22 you’ll tackle Windows authentication, and in Chapter 23 you’ll learn how to restrictauthenticated users with sophisticated authorization rules and use role-based security. In Chapter24 you’ll explore the profiles API, a new, prebuilt solution for storing user-specific information, andin Chapter 25 you’ll go one step further and learn how to protect the data you store in a database aswell as the information you send in a URL with encryption. Finally, Chapter 26 shows how you canplug into the ASP.NET security model by designing a custom membership provider.

Part 5: Advanced User Interface: This part shows how you can extend web pages with advancedtechniques. In Chapters 27 and 28 you’ll tackle custom controls. In Chapter 29 and Chapter 30you’ll branch out to use JavaScript for dynamic pages and GDI+ for handcrafted graphics. Finally,Chapter 31 explores the ASP.NET 2.0 Web Parts Framework for creating web portals.

Part 6: Web Services: Web services promise to revolutionize the way functionality is shared acrossdifferent applications, network environments, and computing platforms. In Chapter 32 you’ll startat the beginning; you’ll see how to create basic web services and how to use them in ASP.NET webapplications, .NET Windows applications, and even legacy ASP applications. In Chapter 33 you’lltake a low-level look at the standards that make it all possible and see how they work. In Chapter 34you’ll learn how to use advanced techniques to call web services asynchronously, implement secureservices, and start working with newer web service standards using the WSE (Web ServicesEnhancements) toolkit.

■INTRODUCTIONxxxiv