Microsoft ASP.Net Setup and Configuration Pocket Reference James Avery PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2003 by ActiveEducation All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Cataloging-in-Publication Data Avery, James. Microsoft ASP.NET Setup and Configuration Pocket Reference / James Avery. p. cm. Includes index. ISBN 0-7356-1936-0 1. Active server pages. 2. Microsoft .NET. 3. Web site development--Computer programs. I. Title. TK5105.8885.A26A94 2003 005.2'76--dc21 2003043009 Printed and bound in the United States of America. 123456789
QWE 8 7 6 5 4 3
Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to
[email protected]. FrontPage, JScript, Microsoft, Microsoft Press, MSDN, Visual Basic, Visual Studio, Windows, and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. Acquisitions Editor: Anne Hamilton Project Editor: Kristen Weatherby Technical Editor: Keith Bednarczuk Body Part No. X09-45932 About the Author
James Avery is a .NET consultant working in the Cincinnati, Ohio, area. James has been developing applications since 1997 and currently works with ASP.NET, C#, and SQL Server 2000 to build enterprise Web solutions. Before becoming a consultant, James worked for numerous companies, including Schawk Inc. and Dell Computer. James has written numerous books and articles; he recently coauthored Professional ASP.NET Performance, published by Wrox Press. He has written articles on ASP.NET performance and ASP to ASP.NET migration for ASPToday, as well as maintaining a column at aspalliance.com and contributing to 4guysfromrolla.com. You can also find him answering questions on the ASP.NET forums and the ASP Advice lists. James currently lives in Cincinnati, Ohio, with his fiancée Tammy Glasgow. In his spare time, he enjoys gaming, digital photography, and blogging. You can read his Web log at www.dotnetweblogs.com/Javery. At Microsoft Press, we use tools to illustrate our books for software developers and IT professionals. Tools very simply and powerfully symbolize human inventiveness. They're a metaphor for people extending their capabilities, precision, and reach. From simple calipers and pliers to digital micrometers and lasers, these stylized illustrations give each book a visual identity, and a personality to the series. With tools and knowledge, there's no limit to creativity and innovation. Our tagline says it all: the tools you need to put technology to work. To Tammy, Only through your love, support, and patience was any of this possible. I will love you always. Acknowledgments This book would not have been possible without the hard work of a lot of people. First I have to thank Robert Kern, my agent, for making this book possible in the first place. I want to give special thanks to Anne Hamilton at Microsoft Press for giving me the original idea for this book and in signing me on to write it. Without these two people, this project would never have found its way to my desk. Thanks to all the other people at Microsoft Press including Kristen Weatherby, and the editors at Online Training Solutions, Inc. including Nancy Depper and Keith Bednarczuk. I also want to thank Dave Wanta for making the rotor source code easily available on his site at www.123aspx.com/rotor; without this I would not have realized how invaluable this code is. And last but not least, thanks to my fiancé Tammy for helping me in so many different ways.
Table of Contents Microsoft ASP.Net Setup and Configuration Pocket Reference Introduction Part I - Overview of Microsoft ASP.NET Configuration Chapter 1 - Introduction to the Microsoft ASP.NET Configuration Chapter 2 - Examining the ASP.NET Configuration Architecture Part II - ASP.NET Configuration Settings Chapter 3 - Configuration Section and Application Settings Chapter 4 - System.Web Configuration Settings Chapter 5 - Mobile Controls Configuration Settings Chapter 6 - Microsoft .NET Configuration Settings Chapter 7 - CLR Configuration Settings Chapter 8 - IIS Settings Part III - Creating Custom Settings Chapter 9 - Creating and Using Custom Sections Part IV - Appendix and Glossary Appendix - Microsoft .NET Framework Configuration Tool Glossary List of Figures List of Tables List of Code Examples List of Sidebars Index
Introduction Overview Welcome to the Microsoft ASP.NET Setup and Configuration Pocket Reference, the definitive quick reference guide to working with the ASP.NET configuration files. ASP.NET and the .NET Framework incorporate a very powerful and adaptable configuration model based on XML configuration files. The XML configuration files contain various configuration sections and elements that contain settings for servers and applications. This book provides an easy-to-use quick reference for the many configuration sections, settings, and possible values. This guide will illustrate how to use this powerful configuration model to modify an application’s behavior and add functionality. This guide is designed to provide you with a single resource about ASP.NET configuration. This book is not an attempt to be a complete reference to the configuration files, but rather a complete reference to ASP.NET configuration. The step-by-step procedures, examples, and reference tables will help you answer your questions and finish the task. You will no longer have to look for solutions in general ASP.NET books or rifle through the documentation; you can turn to this book and quickly find the solutions you need. This guide will cover the following ASP.NET configuration topics. The format and structure of ASP.NET configuration files The architecture of the ASP.NET configuration model The settings in the System.Web group The settings for the Microsoft Mobile Internet Toolkit configuration sections The .NET Framework section groups, including System.Net, System.Diagnostics, and more The settings for the common language runtime The settings that are modified through the IIS administration tool The creation of custom configuration sections and custom section handlers The use of the security configuration tool to apply security settings to .NET assemblies
Who Should Read This Book When I set out to write this book, I thought of what would be useful to me, a developer who works with ASP.NET every day. When I am modifying the configuration of my ASP.NET applications, I often have to turn to the .NET SDK documentation or the Internet for help. What I really needed was an easy-to-use guide to the configuration of ASP.NET applications. I wrote this book for ASP.NET developers who, like me, wanted such a guide. Advanced, intermediate, and beginning ASP.NET developers will all be able to benefit from this guide. To fit the maximum amount of information in this book while keeping it concise and pocketsized, this book is written under the assumption that readers already know the following. How to use ASP.NET to build Web applications How to develop ASP.NET applications with either Visual Basic .NET or C# How to work with Microsoft Windows 2000 Server or Microsoft .NET Server and Internet Information Services
How This Book Is Organized The goal of this book is to provide an easy-to-use guide to ASP.NET configuration. To do this, I have divided the book into logical parts. By looking at the Table of Contents, or even just by flipping through the book, you should be able to find the answer you are looking for. 1
The book is broken down into the following parts and chapters. Part I, Overview of Microsoft ASP.NET Configuration The chapters in this section provide an overview of the ASP.NET configuration files and the architecture that surrounds them. Chapter 1 is an introduction to how the ASP.NET configuration works and a look at the structure and format of the configuration files. Chapter 2 looks “under the hood” of the configuration model to see how the files and settings actually work. Part 1 of this book will give you a better understanding of how ASP.NET configuration works and make it easier for you to use the other parts of this book. Part II, ASP.NET Configuration Settings The chapters in this section are a complete guide to the sections, settings, and values that can be used in the ASP.NET configuration files. Chapter 3 is a guide to working with section declarations and adding or removing values from the
section. Chapter 4 covers the sections and settings for the System.Web namespace, which includes the majority of the settings that you will be using with ASP.NET. Chapter 5 is a guide to the settings that configure the Microsoft Mobile Internet Toolkit, which is now included with ASP.NET 1.1. Chapter 6 is a guide to the settings for the other .NET namespaces, including the System.Net and System.Diagnostics namespaces, among others. Chapter 7 is a guide to working with the configuration sections that contain settings for the .NET common language runtime. Chapter 8 is a walkthrough of the configuration settings that are configured using the Internet Information Services management tool. Part III, Creating Custom Settings This section covers how to use custom settings in the configuration files. Chapter 9 is a complete walkthrough of how to add custom sections and settings and use them in your applications. Part IV, Appendix and Glossary The appendix covers the security configuration tool, which is used to set the security levels of assemblies.
Conventions Used in This Book I’ve used a variety of elements to help keep the text clear and easy to follow. You’ll find code terms and listings in monospace type, except when I tell you to actually type a command. In that case, the command appears in bold type. When I introduce and define a new term, I put it in italics. This book also includes the following elements. To provide additional details on a particular point that needs emphasis.
Note Tip
To offer helpful hints or additional information.
Caution More Info Real World
To warn you when there are potential problems you should look out for. To provide more information on a subject. To provide real-world advice when discussing advanced topics.
I very much hope that Microsoft ASP.NET Setup and Configuration Pocket Reference finds a home on your desk and that you find this book useful while developing ASP.NET applications. If you have any questions or comments, please feel free to send them to me in care of Microsoft. Thank you for reading this book!
2
Support Every effort has been made to ensure the accuracy of this book and the companion content. Microsoft Press provides corrections for books and companion content through the World Wide Web at http://www.microsoft.com/mspress/support. To connect directly to the Microsoft Press Technical Support Knowledge Base and enter a query about a question you might have, go to http://www.microsoft.com/mspress/support/ search.asp. If you have comments, questions, or ideas about this book and do not find your issue mentioned in the Knowledge Base, please contact Microsoft Press using either of the following methods: Postal Mail: Microsoft Press Attn: Editor, Microsoft ASP.NET Setup and Configuration Pocket Reference One Microsoft Way Redmond, WA 98052 E-mail: [email protected] Please note that product support is not offered through the mail addresses. For support information, visit Microsoft’s Web site at http://www.microsoft.com/support.
3
Part I: Overview of Microsoft ASP.NET Configuration Chapter List Chapter 1: Introduction to the Microsoft ASP.NET Configuration Chapter 2: Examining the ASP.NET Configuration Architecture
Part Overview Part 1 covers how the ASP.NET configuration model works and provides a solid overview of the ASP.NET configuration model, which will aid in the understanding of the rest of this book. Chapter 1 details the structure and format of the configuration files, as well as the security and modification detection features of the configuration model. Chapter 1 also includes a walk through of editing a configuration file. Chapter 2 focuses on the architecture of the configuration model and how the settings are interpreted and used by the Microsoft .NET Framework.
4
Chapter 1: Introduction to the Microsoft ASP.NET Configuration Highlights This chapter covers the structure and format of the configuration files, demonstrates how to edit a configuration file, and looks at the security and modification detection of the configuration model. Some of the features and benefits of the ASP.NET configuration model include: The configuration settings are stored in XML files with the .config extension. This enables you to edit the files with a text editor. The configuration files are cached by the Microsoft .NET Framework and not “locked,” which enables you to make changes to live configuration files with minimal impact on users. Settings can be configured in multiple configuration files and propagated from server to application and from parent application to child application. This enables you to configure settings for a server, application, directory, or even a single file. Custom sections and settings enable you to specify your own custom settings, which can be stored in the configuration files and accessed by your applications. Configurations files are secure and cannot be accessed through HTTP requests.
ASP.NET Configuration Overview ASP.NET and the .NET Framework include a very powerful and adaptable configuration model based on XML configuration files. These files contain a large range of different configuration settings that can be specified using well-formed XML. When a change is made to one of the configuration files, the configuration settings are recompiled and recached the next time the file or application is accessed. After the compilation, the configuration files are cached, not locked, so you can continue to modify the files. Because the files are XML, you can use any text editor to edit the files. The two main file types that you will use are called machine.config and web.config. The machine.config file contains the settings for the entire server and the base settings that are used for each web application. The machine.config file can be found in the c:\%winndir%\Microsoft.Net\Framework\version\config directory. Replace version with the installed version of the .NET framework. Note
%winndir% represents the Windows installation directory, whether that is c:\winnt, c:\windows, or another directory.
The web.config file contains settings that are specific to the application and to any child applications or directories. The web.config file for a web application is stored in the root directory of that application. Settings in an application’s web.config file can override the settings contained in the machine.config file. The settings in a particular web.config file can be overridden again by an additional web.config file in a child application of that application. For instance, if tracing is disabled in the machine.config file of a server, that would be the default setting for all the ASP.NET applications running on the server. If you enabled tracing in the web.config file of the ASP.NET web application App1, tracing will be enabled for App1 but still disabled for all other applications because settings in the web.config file take precedence over settings in the machine.config file. If a child application to App1 is added, the child application will also 5
have tracing enabled because it will inherit first from the machine.config file, and then from the web.config file of App1. This is covered in more detail in Chapter 2, but for now remember that server-level settings are stored in the machine.config file and can be overridden by application level settings that are stored in each application’s individual web.config file.
Dissecting the ASP.NET Configuration Files The machine.config and web.config files serve different roles in the configuration architecture, but the format and available settings of the files are virtually identical. Because the files are XML based, there are some XML rules that pertain to the configuration files. XML is case sensitive, so the files are case sensitive as well. Tag sets must have a start tag and an end tag. For instance, must have a closing or be self closing . Tags can have multiple attributes and values, and the values must be contained inside parentheses. Because the files are case sensitive, it is important to know the two different conventions that are used in the configurations files: Camel caseEach word is all lower case except for the first letter of appended words. For example, appSettings, maxRequestLength, userName. Pascal caseEach word starts with a capital letter, including the first and any appended words. For example, PublicKeyToken, RemoteOnly, AutoGenerate. This stripped down machine.config file shows the different sections and general structure of the file. <sectionGroup name="system.web"> <section name="httpRuntime" type ="System.Web.Configuration.HttpRuntimeConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <section name="compilation" type= "System.Web.Configuration.CompilationConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <system.web>
maxBatchSize="1000" maxBatchGeneratedFileSize="3000" numRecompilesBeforeAppRestart="15">
language="vb;vbs;visualbasic;vbscript"
".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> At the beginning of the machine.config file is an XML declaration. This specifies that the file is an XML 1.0 compliant document and uses UTF-8 encoding. The next line is the start of the configuration data. This is the opening tag of the tag set that contains all the different configuration sections and settings. At the end of the file there is a tag. All the sections and settings must be in between these two tags. There are a variety of section types located between the configuration tags.
Configuration Section Handler Declaration The section is first in the configuration files. This section provides configuration section handler declarations for each of the configuration sections that follow. Each configuration section is linked to a handler that will process the settings for that section. Each declaration links its configuration section to a handler that will process the settings for that section. A configuration handler is a class that implements the IConfigurationSectionHandler interface and interprets the different settings and values of a configuration section. More Info
An interface is a kind of .NET blueprint. The interface contains methods that the implementing class must contain. In this example, the class that implements IConfigurationSectionHandler must contain the Create() method. 7
Let’s take a closer look at one of these declarations. <section name="httpRuntime" type= "System.Web.Configuration.HttpRuntimeConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> Thename=setting specifies the name that will be used when declaring the section later in the configuration file. In this example, it is httpRuntime. The type=setting specifies the handler that will process the section and interpret its values. In this example, the handler is the class System.Web.Configuration.HttpRuntimeConfigurationHandler. The type=value is the GAC info needed to locate the correct class. More Info
The GAC, or Global Assembly Cache, is the .NET Framework’s method of storing references to different .NET assemblies that need to be available to multiple applications.
Inside is also where section groups are declared. You can see that multiple declarations are grouped under a section group like this. <sectionGroup name="system.web"> <section name="httpRuntime" type= "System.Web.Configuration.HttpRuntimeConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <section name="compilation" type= "System.Web.Configuration.CompilationConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <system.web> 8
This means that the sections named httpRuntimeandcompilationmust be located in the system.websection group. Declaration handlers are covered in detail in Chapter 2, and creating custom handlers are covered in Chapter 9.
Section Groups As shown in the declarations section, different configuration sections can be grouped into section groups for organizational purposes. This makes it much easier to read and understand all the different configuration sections. Here’s how sections are grouped into groups. <system.web> Different sections are grouped inside tags representing the declared section group name. In this example, the httpRuntime and compilation sections are grouped under the system.web section group.
Configuration Sections Configuration sections are the meat of the configuration files; these sections are where all the different settings are configured. The sections use the name that was declared in the section handler declaration and can contain multiple settings, values, and sub-sections. This is a configuration section from the sample machine.config. This section groups a number of different settings like executionTimeout and maxRequestLength that are all related to the HTTP runtime. These settings are also all handled by the same configuration handler, which is specified in the handler declaration area. The section name is always presented in Camel case.
Sub-Sections Sections can also contain sub-sections. A sub-section is a setting that allows multiple values. Sub-sections are part of the parent section and handled by the same configuration handler. This example compilation section contains a number of different sub-sections.
numRecompilesBeforeAppRestart="15"> The section name is compilation and it contains a sub-section for compilers and multiple sub-sections named compiler. There is no limit to the number of sub-sections that a section can contain. Sub-section names should always follow the Camel case convention.
Settings Configuration settings are found in either sections or sub-sections and include the name of the setting followed by an equal sign and a value enclosed in parentheses. Here is a setting and its value. The name of the configurations section is compilation, the name of the settings is debug, and the value of the setting is set to the Boolean value false. The names of settings almost always follow the Camel case convention. The values generally follow the Pascal case convention, but there are a couple of exceptions. The words true and false are always lower case. The value of a setting can also be a literal string, like a database connection string, and in that instance, it does not conform to any casing standard.
Applying Configuration File Modifications When a user first accesses a URL, ASP.NET looks at the machine.config file and any applicable web.config files and determines what configuration settings should be applied to that URL. This information is then cached so that on the next request to that URL, the information is available without having to read from the configuration files again. When a change is made to an applicable configuration file, the cached settings are removed, and the next time the URL is accessed, the process starts over again. 10
Because the applications do not have the files open while they run, you can freely modify the files without worrying about stopping the application. After the changes have been made, you can simply save the file, and the next time the affected page is accessed, the settings will be applied—virtually seamless to the users visiting the site. There is no compilation of the configuration files, which can be both a good thing and a bad thing. It is a good thing because it makes it possible to edit the files with a normal text editor and does not require any sort of special application or compiler. It can be a bad thing because there is not a step between the modification of the file and the application recompiling with the new configuration. If there is an incorrect value, the application will throw an error on compilation, or if the modification was in the machine.config file, all the applications would throw an error. This would make the application unavailable to users until the error could be corrected. For this reason, it is always a good idea to test configuration changes on a development server before performing the change on the production server.
Looking at Security Files with the .config extension cannot be accessed through a browser. Any attempts to access the files are responded to with an HTTP 403 Access Denied Error. This is the same level of protection that is applied to .aspx files. It is obvious why you would not want these files to be viewable by the public, and this level of security ensures that they can’t be. Because the general public cannot access this information, you can store database connection strings, passwords, and license keys in the configuration files with confidence that they will be secure.
Editing Configuration Files Let’s take a quick look at editing a configuration file, which is a pretty simple procedure. If you have edited configuration files before, you can skip this section. 1. Navigate to C:\winnt if you are running Windows 2000 or C:\Windows if you are running Windows XP. 2. Open the Microsoft .NET Directory. 3. Open the Framework Directory. 4. Open the directory that is named after the version of the Framework that you have installed, for example, “v1.1.4322”. 5. Open the CONFIG directory where the different .config files are available for editing. 6. Right-click the machine.config file, click Open With on the shortcut menu, and click Notepad in the list of applications. As shown in Figure 1-1, you can now see the XML text of the configuration file.
11
Figure 1-1: The machine.config file in Notepad. Because the files are XML, you can make changes right in Notepad. For this example, you are going to enable tracing for all the applications running on this server. To do so, you will need to find the trace configuration section, which can be seen in Figure 1-2.
Figure 1-2: The value that needs to be changed to enable tracing is highlighted. After you’ve found the trace configuration section, change the value of the enabled= setting from false to true. Doing so will enable tracing for all applications, unless of course 12
the web.config of that application overrides this setting. All you need to do now is save the configuration file. The modification of the file will be detected and the next time a user accesses an affected URL, the settings will be recompiled and cached again. Caution
Remember that there is no error-catching layer in this editing process. If you had set the value to truth instead of true, all the applications would fail. When a configuration handler encounters a problem, it throws a configuration error that will be displayed to users. You should be extra careful during the editing process, and if at all possible, apply changes to a stage or development server before making changes to the production server.
Configuring IIS What’s this? IIS? Internet Information Services? I thought we were done with changing settings and configuration applications in IIS. Although most settings for ASP.NET applications are configured in the .config files, you still have to use the Internet Information Services (IIS) admin tool for some of your configuration changes. When a user initiates a Web request, it is handled by IIS first. If it is an ASP.NET request, it is then routed to the ASP.NET runtime for processing. Because of this, some of the front-end settings still have to be configured in IIS so the request can be properly routed. To make the following changes, you will need to use the IIS admin tool. Creating and removing applications Setting and modifying security settings for applications Modifying file extensions handled by ASP.NET When working with Windows Server 2003, the ASP.NET process configuration settings are configured in IIS rather than the configuration files. How to make these changes is covered in Chapter 8.
Key Points ASP.NET Configuration files are XML based and have certain XML rules that must be followed, including case sensitivity, beginning and ending tags, and attributes enclosed in parentheses. The machine.config file settings apply to the entire server, whereas web.config files are application specific. If settings in an application’s web.config conflict with the serverwide machine.config settings, the web.config setting takes precedence. There are Section Handler Declarations, Section Groups, Sections, and Settings and Values all included in the configuration files. When configuration files have been modified, the changes are detected by the Framework and are applied when the affected files are next accessed. Configuration files are secure and cannot be accessed by HTTP requests. If it is attempted, an Access Denied Error is returned to the user. This chapter covered the basics of ASP.NET configuration. The next chapter looks under the hood of the ASP.NET configuration model and provides a better idea of how the configuration architecture works.
13
Chapter 2: Examining Architecture
the ASP.NET
Configuration
Overview The first chapter provided a quick overview of ASP.NET Configuration. This chapter takes a closer look at the configuration architecture and how it works. Topics covered in this chapter include: The rules of the hierarchical model, and how you can apply settings to a server, an application, a directory, and even a single file. How configuration sections are processed and how the values are accessed by applications. The internals of a sample configuration section handler.
Hierarchical Configuration Model As discussed in Chapter 1, the ASP.NET configuration model inherits settings from server to application through a hierarchical model. With a hierarchical model, you can specify settings in the machine.config or web.config file of a parent application, and those settings will propagate to any child applications. Child applications can inherit from parent applications, and all applications inherit from the machine.config file. You can specify settings for an entire server, a single or multiple applications, single or multiple directories, or even a single file. The following rules apply to the inheritance of configuration settings.
Applications first inherit their settings from the machine.config file of the server, then from the web.config file of any parent applications, and finally from their own web.config file. The settings in each web.config file override the settings from the machine.config and web.config files before it. Inheritance follows the URL of the requested resource and not necessarily the physical structure of the files. The settings in the machine.config file or a parent application’s web.config file can prevent settings from being overridden. The settings can be targeted to a specific directory, application, or file using the location setting.
To better demonstrate these inheritance rules, let’s take a look at a scenario involving multiple applications and directories. For this example, you will use three applications: App1, App2, and App3. Figure 2-1 is an overview of the virtual structure of these applications. More Info
The virtual structure of the applications is the structure when viewed through the IIS admin tool. This structure is not tied to the physical location of the directories and is simply how you set up the applications in IIS.
14
Figure 2-1. The virtual structure of the sample applications. In Figure 2-1 , App1 and App3 are root applications, and App2 is a sub-application of App1. More Info
App3 is a virtual directory in IIS. A virtual directory is a pointer from IIS to the real physical directory on the drive. The other applications are not virtual directories because they appear in the same location under the wwwroot on the physical structure as they do in the virtual structure.
The inheritance in the hierarchical model looks first at virtual structure, and then at the physical structure of the applications. Figure 2-2 shows the physical structure of the applications. Figure 2-2 shows that App1 is a sub-directory of the wwwroot directory, and that App2 and App3 are sub-directories of App1. You will notice that SubDir is also a sub-directory of App1, but it is not an application or virtual directory in IIS. 1. Enable tracing in the machine.config file on the server by following the steps in “Editing Configuration Files” in Chapter 1. By making this change in the machine.config file, tracing will be enabled for all the applications and .aspx files on the server. 2. Disable tracing in the web.config file of App1, the first Web application.
15
Figure 2-2. : The physical structure of the sample applications. You will see the following results on the following URLs when they are accessed. In this example we are using http://www.northwindtraders.com/ as the site name. Replace this with your site name when testing.
http://www.northwindtraders.com/App1 This URL inherits enabled tracing from the machine.config file, but the setting in the web.config file of App1, which disables tracing, overrides it. Therefore, tracing is disabled for this URL. http://www.northwindtraders.com/App1/App2 Because App2 is a sub-application of App1 in the virtual structure, tracing is disabled here also because App2 inherits from the web.config file of App1, and there is no conflicting setting in the web.config file of App2. http://www.northwindtraders.com/App1/SubDir All files in the SubDir directory have tracing disabled because they inherit the setting from the web.config file of App2. http://www.northwindtraders.com/App3 App3 still has tracing enabled because it inherits only from the machine.config file of the server. Even though it is a physical child of App1, it is not a virtual child of App1. http://www.northwindtraders.com/App1/App3 App3 is its own application, but it is still a sub-directory of App1 and can be accessed by that path. As a result, App3 has tracing disabled because it inherits from the web.config file of App1.
Note
This adds an interesting dimension because a single directory or application can have multiple configuration settings based on which URL is being used to access the resource.
The URL of the resource determines which configuration settings the resource will inherit. In other words, identical resources can inherit different configuration settings depending on the URL used to access them. With this understanding of how configuration settings are inherited from server to applications, let’s take a look at how to target configuration settings.
Targeting Configuration Settings By using the location tag, you can target settings to a particular path, application, or file. This is particularly useful when you want to use a different setting for a physical directory but don’t want or need to make that directory an application. For example, you might want a single directory to have different trace settings. You can accomplish this by using the location tag and adding the following text to the web.config file of App1. 16
<system.web>
<system.web> This disables tracing for all the files in the SubDir directory. Note
Notice that you will still need to include the system.web section group tag inside the location tag. Whenever you use the location tag, the name of the section group must be included.
Tracing would also be disabled for any sub-directories of the SubDir directory. You can also use this procedure to assign configuration settings to a single file. This disables tracing for the single file notracepage.aspx in the root directory of App1. Note
Using the location tag can be very useful when you want to set security permissions for a single file in your application. Keep in mind that this will only work on files that are handled by the ASP.NET process, which means that you can’t apply configuration settings to html or graphics files because they are handled by IIS and not ASP.NET.
Preventing Configuration Settings from Being Overridden As you have seen, you can override settings in the web.config file of an application. However, there are times when you might like to prevent settings from being overridden. Luckily, there is a setting that enables you to do this. By setting allowOverride to false in either the machine.config or web.config file, no matter what you specify in the inheriting web.config file of the application, in this example, tracing will always be disabled. <system.web>
You can also use this in conjunction with the location tag. Real World
Preventing configuration settings from being overridden can be particularly useful if you are an administrator or run a hosting company. It would enable you to set global settings for your server that cannot be changed by your customers in the individual applications.
Keep in mind that if you use the allowOverride=”false” property, any application that attempts to override this setting will throw a configuration error. Be sure that none of the applications are attempting to set this value before you set it to allowOverride=”false” or using this property will break those applications.
Examining Configuration Section Handlers So far, I have covered where to specify the settings, how the settings inherit, and how you can target and lock settings. Now let’s look at how the configuration settings are used. Configuration section handlers process the settings that are specified in configuration files and make them available to applications. Configuration section handlers are classes that implement the IConfigurationSectionHandler interface. These classes interpret and process the configuration file settings and return a configuration object based on those settings. Configuration section handlers are first declared in configSections, as discussed in the last chapter. Let’s take a look at the configuration handler declaration again. <section name="appSettings" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
This declaration is for the section of the configuration file. The section is used to store name/value pairs that you want to access from any of the application files. The declaration points the configuration file to the class that will 18
handle this section. Here is a sample section that adds a couple of sample name/value pairs. These settings can then be accessed from the applications, which makes it very simple to keep track of and change settings that you will need to use on different pages or in different applications. You have seen where the declaration handler is declared, but you cannot look at the actual declaration handler code of the .NET Framework. However, you can look at the Microsoft Shared Source CLI Implementation (codename: Rotor) source code for the same handler, which is the next best thing. More Info
The Microsoft Shared Source CLI Implementation, affectionately known as Rotor, is an open source implementation of a substantial portion of the .NET Framework made available from Microsoft. Although it is not the exact code that is used in the .NET Framework, it is very similar and it performs the same function. However, there will be numerous differences because the Rotor code is designed to be more portable and readable. For more information see http:// msdn.microsoft.com/downloads/default.asp?url=/ downloads/topic.asp?URL=/MSDN-FILES/028/000/ 123/topic.xml.
The following is the configuration section handler for the section, which is called NameValueFileSectionHandler. I have removed code unrelated to this discussion. //----------------------------------------------------------------// // //
Copyright (c) 2002 Microsoft Corporation.
All rights
reserved. //
The use and distribution terms for this software are contained in the file named license.txt, which can be found in the root of
// this //
distribution. By using this software in any fashion, you are agreeing
//
to be bound by the terms of this license.
//
You must not remove this notice, or any other, from this software.
// // //----------------------------------------------------------------19
#if !LIB namespace System.Configuration { using System.IO; using System.Xml; public class NameValueFileSectionHandler : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { object result = parent; // parse XML XmlNode fileAttribute = section.Attributes.RemoveNamedItem("file"); result = NameValueSectionHandler.CreateStatic(result, section); if (fileAttribute != null && fileAttribute.Value.Length != 0) { / *Removed for Brevity, this section would handle if there was a file="" property in the appSettings section
*/
} return result; } } } #endif This handler checks if a file=”“ property is present, and then returns a value based on the NameValueSectionHandler.CreateStatic method if that property is blank. This is the method that will create the configuration object. This is the code for the NameValueSectionHandler. //----------------------------------------------------------------// // //
Copyright (c) 2002 Microsoft Corporation.
All rights
reserved. //
The use and distribution terms for this software are 20
contained in the file named license.txt, which can be found in the root of
// this
distribution. By using this software in any fashion, you
//
are agreeing //
to be bound by the terms of this license.
//
You must not remove this notice, or any other, from this software.
// // //----------------------------------------------------------------#if !LIB namespace System.Configuration { using System.Collections; using System.Collections.Specialized; using System.Xml; using System.Globalization; public class NameValueSectionHandler : IConfigurationSectionHandler { const string defaultKeyAttribute = "key"; const string defaultValueAttribute = "value"; public object Create(object parent, object context, XmlNode section) { return CreateStatic(parent, section, KeyAttributeName, ValueAttributeName); } internal static object CreateStatic(object parent, XmlNode section) { return CreateStatic(parent, section, defaultKeyAttribute, defaultValueAttribute); } internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName) { 21
ReadOnlyNameValueCollection result; //
start result off as a shallow clone of the parent
if (parent == null) result = new ReadOnlyNameValueCollection(new CaseInsensitiveHashCodeProvider( CultureInfo.InvariantCulture), new
CaseInsensitiveComparer(
CultureInfo.InvariantCulture)); else { ReadOnlyNameValueCollection parentCollection = (ReadOnlyNameValueCollection)parent; result = new ReadOnlyNameValueCollection(parentCollection); } // process XML HandlerBase.CheckForUnrecognizedAttributes(section); foreach (XmlNode child in section.ChildNodes) { // skip whitespace and comments if (HandlerBase.IsIgnorableAlsoCheckForNonElement( child)) continue; // handle <set>, , tags if (child.Name == "add") { String key = HandlerBase.RemoveRequiredAttribute( child, keyAttriuteName); String value = HandlerBase.RemoveRequiredAttribute( child, valueAttributeName, true /*allowEmptyString*/); HandlerBase.CheckForUnrecognizedAttributes(child); result[key] = value; } else if (child.Name == "remove") { String key = HandlerBase.RemoveRequiredAttribute( child, keyAttriuteName); HandlerBase.CheckForUnrecognizedAttributes(child); result.Remove(key); } else if (child.Name.Equals("clear")) { 22
HandlerBase.CheckForUnrecognizedAttributes(child); result.Clear();
}
else { HandlerBase.ThrowUnrecognizedElement(child); } } result.SetReadOnly(); return result; } protected virtual string KeyAttributeName { get { return defaultKeyAttribute;} } protected virtual string ValueAttributeName { get { return defaultValueAttribute;} } } } #endif This code creates a configuration collection object that contains the different key and value pairs from the configuration section. I won’t spend a large amount of time focusing on this handler’s exact code, but let’s take a look at a couple of the more important parts. The CreateStatic method is called from the first section handler, and it will create the configuration object. This method creates a ReadOnlyNameValueCollection object that will hold all the different key and value pairs of information. Let’s take a look at the code that is used to parse the XML. if (child.Name == "add") { String key = HandlerBase.RemoveRequiredAttribute(child, keyAttriuteName); String value = HandlerBase.RemoveRequiredAttribute(child, valueAttributeName, true/*allowEmptyString*/); HandlerBase.CheckForUnrecognizedAttributes(child); result[key] = value; } If the XML child name is equal to “add,” this code adds the key/value pair to the ReadOnlyNameValueCollection object called result. When you reference the collection in the code, you access this collection to reference the values you are storing in this section. Configuration section handlers interpret and make available all the settings in a particular section. Chapter 9 will cover how to create custom configuration section handlers to interpret the settings for the custom sections. 23
Key Points
The hierarchical configuration model inherits from server to application and from application to application in a variety of ways. This inheritance is based on the URL that is used to access the resource in question. You can use the location tag to target configuration settings to a particular directory or even a single file. You can use the allowOverride setting to prevent a setting from being overridden by a later web.config file. Configuration section handlers make the settings in the configuration available to applications by processing the XML and returning a configuration object.
24
Part II: ASP.NET Configuration Settings Chapter List Chapter 3: Configuration Section and Application Settings Chapter 4: System.Web Configuration Settings Chapter 5: Mobile Controls Configuration Settings Chapter 6: Microsoft .NET Configuration Settings Chapter 7: CLR Configuration Settings Chapter 8: IIS Settings
Part Overview Part II of this book covers all the different ASP.NET configuration settings that you can use to customize applications. Chapter 3 covers the first two sections that appear in the configuration files: the and sections. Chapter 4 covers all the sections and settings directly related to ASP.NET, including all the different sections and settings in the System.Web section group. Chapter 5 covers the sections and settings pertaining to the Microsoft Mobile Internet Toolkit. Chapter 6 covers the settings for the rest of the Microsoft .NET Framework namespaces. Chapter 7 covers the settings for the Common Language Runtime. Chapter 8 covers the settings that you use with the IIS admin tool.
25
Chapter 3: Configuration Section and Application Settings Overview This chapter covers the and sections of the configuration files. You looked at the section in Chapter 1, and this chapter will go into more detail. This chapter also covers the section, which you can use to add settings to the configuration file and then access the settings from your applications. This chapter covers all the different sections, settings, and values for each of these different sections. You will also see how you can use these settings, including examples of common techniques. This chapter covers the following topics.
This section contains all the configuration section handler declarations. This was discussed in the last chapter, but now you will see the different elements that can be used in this section. This section enables you to store strings in a central location. This section uses a key/value pair to store and retrieve the string. You will see how to store these values as well as how to use this in an application.
Working with The section contains all the various configuration section handler declarations. Configuration section handlers are the .NET classes that interpret the settings and values contained in the configuration section. In the section, you can declare a new section, remove a section, and even clear out all the section declarations.
Declaring a Section The first element in the section is <section>. This element is used to declare a configuration section that will be used later in the configuration file. This is the syntax of the <section> element. <section name="SectionName" type="ConfigurationHandler" allowLocation="true | false" allowDefinition="true | false" /> Table 3-1 lists the <section> element’s attributes. Table 3-1: <section> attributes Attribute name
Attribute values
Name
string The name you will use when to refer to this section in your configuration file. This can be any value that does not conflict with another section name but for the most part the Pascal case convention is used to name the sections.
Type
string The configuration section handler. This should include full Global Assembly Cache location information for the class.
AllowLocation
true | false This determines if the section can be used inside a 26
Table 3-1: <section> attributes Attribute name
Attribute values location tag. A value of true means that the section can be used inside a location tag. A value of false means the section cannot be used inside a location tag.
AllowDefinition
Everywhere This value allows the section to be used in any type of configuration file. MachineOnly This value allows the section to be used only in the machine.config file. MachineToApplication This value allows the section to be used in either the machine.config file or the application configuration file.
This is an example of how to add a section to the configuration file. <section name="runtime" type="System.Configuration. IgnoreSectionHandler, Culture=neutral,
System,
Version=1.0.330.0,
PublicKeyToken=b77a5c561934e089" allowLocation="false" /> This example is the section declaration for the runtime section. In the name attribute, the name of the section is specified, which in this example is runtime. The type attribute points to the configuration section handler that will interpret this section; in this example, it is System.Configuration.IgnoreSectionHandler.
Declaring a Section Group The <sectionGroup> element declares a section group and the declarations for all the included sections. A section group is a logical grouping of multiple sections. In configuration files, each namespace that you will be configuring will have its own section group. Generally, machine.config files have various section groups including System.Web and System.Net. If you are going to include multiple configuration sections for your namespace, it is a good idea to place those sections in a section group. This is the syntax for declaring a section group. <sectionGroup name="section group name" > Table 3-2 displays the attribute for the <sectionGroup> element. Table 3-2: <sectionGroup> attributes Attribute name
Attribute value
Name
string The name of the section group. These names generally follow the Pascal case convention.
The following example declares the System.Web section group and includes a section declaration for the section. <sectionGroup name="System.Web"> <section name="httpRuntime" type= 27
"System.Web.Configuration.HttpRuntimeConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> Later in the configuration file, you will include the section inside the <sectionGroup>. <system.web> Removing a Section You can use the element to remove either sections or section groups from your configuration file. This is the syntax of the element. Table 3-3 lists the attribute for the element. Table 3-3: Attributes Attribute name
Attribute value
Name
string The name of the section or section group that you want to remove.
This is how to remove a section declaration that is inherited from an earlier configuration file. This example will remove the section from the group, which will prevent you from configuring the httpRuntime settings in the configuration file or any configuration files that inherit from it.
Clearing All Sections To remove all the sections and section groups, use the element. This element will remove all sections declared before it and any sections declared in earlier configuration files. This is how to use the element in the configuration files. _Machine.Config File‡ <section name="httpRuntime" type= "System.Web.Configuration.HttpRuntimeConfigurationHandler, System.Web, Version=1.0.3300.0, Culture=neutral, 28
PublicKeyToken=b03f5f7f11d50a3a" />
_web.config File‡
In the configuration file, the section would no longer be available. Although the section is not one of the more common sections when working with ASP.NET, it is very important to understand its function in the configuration architecture. Part III of this book looks at this section in even greater detail when you create your own sections and section handlers.
Working with The section enables you to store literal strings in the configuration file and retrieve these values from any application. The strings, or values, are stored with a key that is used to retrieve the string in the applications. Let’s look at the different elements that you can use in the section. Later you will look at the file attribute you can use to point to an XML file containing various elements.
Adding a Value When you have a string that you want to access from various files in your application, you can add it to the section of your configuration file. If you want the value to be available to all the applications on your server, you can also add it to your machine.config file. To add a value to the collection, you can use the element. To add a value, set a key that identifies the value in your collection and the actual value that you want to store. Use the following syntax to incorporate the element. Table 3-4: attributes Attribute name
Attribute value
Key
string This is the unique key that you will use to access the value in the collection. If you add a key that already exists in the collection, you will overwrite that value.
Value
string This is the value that you will store in the configuration 29
Table 3-4: attributes Attribute name
Attribute value file.
This example demonstrates how to add a database string to the configuration file: In this example, you add a key/value pair to the collection. The key is ConnString, and the value of the pair is the server connection string. At the end of this section, you will see how to access this value through ASP.NET. Real World
Being able to store a value like a database connection string in a central location is invaluable to good application design. If this connection value where to change, whether you are moving to a different server or simply changing the user name, you can change the string in one location and it will be updated everywhere. You don’t even need to recompile the applications because the changes take affect the next time the resource is accessed.
Removing a Value The tag is used to remove a key/value pair from the collection. You specify the key of the pair that you want to remove, and the specified pair will no longer be available through the collection. This can be very useful when you want to remove a setting that has been added to the collection in a higher level. For instance, a key/value pair that is added in the machine.config file can be taken out at the web.config level using the following syntax. Table 3-5 lists the attribute for the element. Table 3-5: attributes Attribute name
Attribute value
Key
string The key of the key/value pair that you want to remove from the collection.
The following is an example of how to remove the string that you added in the last section. This key/value pair will no longer be available to this application or any of the applications that inherit from this web.config file.
30
Clearing All Values The element can be used to completely clear out the collection. Like the element, you can use the element to remove different key/value pairs, but the element removes all the pairs. This is how you can use the element. Adding this element to the section completely clears it out.
Using the File Attribute The element includes only one attribute, the file attribute. The file attribute can be used to point to an XML file that contains , , or elements. This method is very similar to a server-side include in that it includes only the settings from the XML file. To use the file attribute, set the value in the section tag. Table 3-6 lists the attribute for the section.
Table 3-6: attribute Attribute name
Attribute value
File
string A relative path to an file. This path is relative to the application root of the ASP.NET application. Values can still be added to the section, but if there is a conflict, the values loaded from the file will override the values added in the section.
This is how to use the file attribute. To use the file attribute, you will need to create an XML file that contains numerous key/value pairs. This is a sample settings file that you can use in conjunction with the file attribute: As you can see, this file is a miniature configuration file with a single configuration section. Inside this section, the child elements are treated just as if they were written in the web.config or machine.config file. This is a very effective technique when you want to include the same values in multiple applications because you can create a file like this one and store it in a central location where all applications can access it.
31
Reading Values Now let’s take a look at how to read values from the section in ASP.NET. The configuration section handler for the section makes these settings available through a name value collection called <AppSettings>. This is the code that accesses the values in both VB.NET and C#. VB.NET Dim sConnString as string 'Create our string value 'Next we read the value from the collection sConnString = Configuration.AppSettings("connString") Dim oConn as SqlConnection 'Create our connection object 'Use our string to create the connection oConn = new SqlConnection(sConnString)
C# \\First we read our value from the AppSettings collection string sConnString
= Configuration.AppSettings["connString"];
\\Then we use our value to create our connection SqlConnection oConn = new SqlConnection(sConnString);
As you can see, it is very easy to access the values that you added to the configuration file. Simply access the <AppSettings> collection and use the name that you specified in the configuration file. The section makes it easy for you to add various values to the configuration file. This is particularly valuable when working with values that you will use on many different pages because you will not have to change the value on each page. Instead, you change it in the configuration file and each page will read the new value.
Key Points
You can use the section to declare sections and section groups. You can use the and elements to remove sections or section groups declared in an earlier configuration file. You can use the section and the element to add values to a collection that can then be accessed from various files or applications. You can use the and elements to remove and clear values that are declared in earlier configuration files.
The next chapter covers how to configure the System.Web namespace.
32
Chapter 4: System.Web Configuration Settings This chapter covers all the different settings available in the <System.Web> section group. This section group contains a large number of settings that can configure ASP.NET, and this chapter examines them all. You will spend the majority of your time working in this section group. The following sections are covered in this chapter.
This section configures the type of authentication employed for your ASP.NET applications. This section configures the authorization of your Web applications. Authorization is the act of determining whether a user should be allowed to access the resource. This section defines the browser capabilities search strings. It works with the browser capabilities component to determine the type of browser being used to access your Web application. This collection-based section contains aliases that link to a specific user agent. These aliases can force a page to render for a specific browser. This section contains settings that configure the compilation of ASP.NET pages. You can use this section to add new programming languages that can be used with ASP.NET. <customErrors>This section customizes the error messages that are sent to users. This section configures the globalization settings for the server or application. This section configures and adds new HTTP handlers. HTTP handlers are the classes that handle requests to various file extensions. This section configures and registers additional HTTP modules. HTTP modules are used to perform operations on the HTTP request before the HTTP handler handles it. This section contains settings that configure the HTTP runtime. This section configures and implements impersonation for your Web applications. <machineKey> This section configures the encryption keys that will be used by the other sections. <pages> This section contains settings that configure the page-level settings for all the pages affected by the configuration file. <processModel> This section contains settings that configure the ASP.NET ISAPI extension. This section is not used when running Windows Server 2003. <securityPolicy> This section declares mappings of named security levels to the correct security policy file. <sessionState> This section configures how session information will be stored for your Web applications. This section configures how the ASP.NET trace service behaves. <trust> This section configures the security level under which the application runs. <webControls> This section configures where the client-side validation script file is located. <webServices> This section contains settings that configure the XML Web services created using ASP.NET.
Working with the Section Authentication is the process of verifying the identity of the user accessing an application. Authentication does not determine if the user is allowed to access a particular resource; that is done by the section in the <System.Web> system group. 33
Authentication verifies only a user’s identity so authorization can determine if that user can access the requested resource. Authentication plays a huge role in developing ASP.NET applications, and in this section, you see how to configure the authentication method used for your application. By working with the configuration files, you can configure your application to use a number of authentication methods. The authentication modes available include the following.
Windows Authentication This method of authentication relies on Microsoft Windows to validate a user. Windows requests a user name and password when a user accesses a site, and then validates the user name and password against the Windows users. This method is very popular on intranets because most users already have domain accounts that can be used for authentication. Forms Authentication This method of authentication uses a basic ASP.NET form to accept a user name and password that can be verified against a database or XML file. This method is popular on Internet sites because it does not rely on the operating system user database. Passport This method of authentication enables users to log into a site using their Microsoft Passport. A Microsoft Passport is a single user name and password (administered by Microsoft) that can be used at many different sites.
To configure the authentication of your application, you will work with the section, which is contained in the <System.Web> section group. This section contains a number of settings and elements that you can use to configure the authentication of your application. Unlike most sections contained in the <System.Web> section group, this section can be used only in the web.config file for your root application. It cannot be configured for an individual directory or file.
Configuring the Authentication Mode The first step in configuring the authentication for your application is to set the mode of authentication. To do this, you need to use the mode property of the element. Here is the syntax of the mode property. The mode property has a number of values that you can use to set different authentication modes. The values are listed in Table 4-1.
Table 4-1 : mode property values Property value
Description
Windows
This value specifies Windows authentication mode. This value should be used whenever Windows is providing user authentication.
Forms
This value specifies forms authentication mode.
Passport
This value specifies that Microsoft Passport is used authenticate users.
None
This value sets the authentication mode to none; that is, no 34
Table 4-1 : mode property values Property value
Description authentication mode is used.
Configuring Authentication Mode for Windows Authentication The first step to setting the mode of authentication to Windows is to add the following line to your web.config file. This is the only change needed in your web.config file. The next step is to set up the method of Windows authentication you will use. To set up Windows authentication, you will use the IIS administration tool. You can use this administration tool to set up Basic, Digest, or Integrated Windows Authentication. Because this section is about using the configuration files, I won’t cover how to set up Windows authentication in the IIS admin tool here. Chapter 8 covers the IIS admin tool and configuring Windows authentication. Configuring Authentication Mode for Forms Authentication In forms authentication, users submit their user name and password for authentication in an ASP.NET form. You will use a variety of sections and settings to set the authentication mode of your application to forms authentication. The ASP.NET configuration files make it easy for you to use and even automate some of the tasks involved. The first step is to set the mode to forms. The next step involves a new element that is used in the section: the element. The element contains properties with settings that you need to configure before using forms authentication. These settings configure the authentication cookie that authenticates a user. Here is the syntax for this element.
Table 4-2 lists the properties, descriptions, and possible values of the element. Table 4-2: properties Property name
Description
name
This property is set to the name of the cookie that is used for authentication. The default value is .ASPXAUTH, but if you have multiple applications running on the same Web server, you will need to customize this value so different cookies are used for each application. 35
Table 4-2: properties Property name
Description
loginUrl
This property is the URL of the page that contains the logon for your site. Requests will be redirected to this page when an unauthenticated user tries to access a page. The default value for this property is default.aspx.
protection
This property specifies the type of encryption used for the authentication cookie. Possible values include: AllSpecifies that the cookie should use both data validation and encryption. This is the default value. NoneSpecifies that both validation and encryption are disabled for cookies. This can be very useful when cookies are not necessarily essential to the application but you are using them to personalize a site. EncryptionSpecifies that the cookie will be encrypted using Triple-DES or DES, but it will not have data validation performed on it. ValidationSpecifies that the cookie will be validated by a scheme that verifies that its contents have not been altered in transit. The cookie is created by concatenating a validation key with the cookie data, computing a message authentication code (MAC), and appending the MAC to the cookie. This protection value ensures that the information has not been tampered with or altered in an attempt to compromise the security of the site.
timeout
This property specifies the time in minutes after which the authentication cookie will expire.
path
This property specifies the path used for the authentication cookie. By default this value is (/).
requireSSL
This property specifies whether SSL is required to transmit the cookie. The possible values are: trueSpecifies that SSL is required. falseSpecifies that SSL is not required. The default value is false.
slidingExpiration
This property specifies whether sliding cookie expiration is enabled. Sliding cookie expiration resets a cookie’s timeout on each request to the application. If a cookie has a timeout of 10 minutes and sliding expiration is not enabled, the cookie expires 10 minutes from when the cookie was first assigned. If sliding cookie expiration is enabled, the cookie expires 10 minutes from the last request to the application. trueSpecifies that sliding expiration is enabled. falseSpecifies that sliding expiration is disabled. The default value is false. This property is new to ASP.NET 1.1.
Here is an example of the element. 36
In this example, I set the name of the cookie to .SiteCookie, the URL of the logon page to login.aspx, and the protection level to all. Setting the protection level to all means that the cookie will be protected through both data validation and encryption. The timeout for the cookie is set to 30 minutes, the path to “/,” and SSL is not required. Sliding Expiration is set to true, which means that the timeout for the cookie will be reset for each request. The timeout value for the cookie is a very important setting. Nothing is more frustrating than filling out a long form and submitting it only to find out that your cookie has expired and the information might be lost. Be sure to either make the timeout long enough or break up long forms across a number of pages.
Real World
Before I discuss how to incorporate forms authentication in your application, let’s look at the sub-element. The sub-element of the element contains a list of user names and passwords that can be used to authenticate users. This is not the only option for maintaining a list of user names and passwords, but it is one of the easier methods and it can be very useful for sites with a small, rarely changed, user/password list. To use the section, first set the passwordFormat property of the element. Here is the syntax for the and passwordFormat property. Table 4-3 lists the available values for the passwordFormat property. Table 4-3: passwordFormat values Value
Description
Clear
This value specifies that the passwords are stored in plain text with no encryption.
MD5
This value specifies that the passwords are stored using the MD5 hash algorithm.
SHA1
This value specifies that the passwords are stored using the SHA1 hash algorithm.
After the method of storing passwords is set, user name and password pairs can be stored in the section using the <user> element. Here is the syntax of the <user> element. <user name="user name" password="encrypted password" / > <user name="user name2" password="encrypted password" 37
/>
The name property is the user name, and the password property is the user’s encrypted password. You can store a group of user names and passwords in the section and then authenticate against this group from your application. Implementing Forms Authentication Now let’s look at the ASP.NET code necessary to use form authentication. Here are the configuration settings for this example. <user name="James" password="HisPassword" /> <user name="Tammy" password="HerPassword" /> The next step is to build your logon.aspx page. This page will accept the user name and password and then authenticate the information against the information in your configuration file. Here is the form you will use to capture the user name and password. This is just a simple ASP.NET form that accepts a user name and password, and runs a server-side event when the login button is pressed. Here is the code used to validate this information against the user names and passwords stored in the configuration file. C# <script language="C#" runat="server"> void btnLogin_OnClick(object sender, EventArgs e) { if(FormsAuthentication.Authenticate(tbUserName.Text, tbPassword.Text) { FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, 38
cbPersist.Checked); } else { lblResults.Text = "Invalid Username and Password"; } }
VB.NET <script language="vb" runat="server"> Sub btnLogin_OnClick(sender as object, e as EventArgs) If FormsAuthentication.Authenticate(tbUserName.Text, _ tbPassword.Text) Then FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, _ cbPersist.Checked) Else lblResults.Text = "Invalid Username and Password" End If End Sub
Let’s look at this code step by step. If FormsAuthentication.Authenticate(tbUserName.Text, _ tbPassword.Text) Then First this code checks if the supplied user name and password authenticate against the stored user names and passwords. Because in this example the user names and passwords are stored in the configuration file, you use the FormsAuthentication.Authenticate() method to authenticate the supplied user name and password against the pairs in the configuration file. For larger, more robust applications, the user names and passwords will need to be stored in a database where they can be modified quickly. When storing user names and passwords in a database, you will need to create a method that checks for a matching pair in the database and returns a value of true or false. This method could be used in place of the FormsAuthentication.Authenticate method and this example would work nicely. If either the FormsAuthentication.Authenticate method or your custom method returns a true value, the following line is processed.
FormsAuthentication.RedirectFromLoginPage(tbUserName.Text, _ cbPersist.Checked) 39
This line of code calls the FormsAuthentication.RedirectFromLoginPage method. This method returns the user to the referring page (the page that the user form transferred from, which is stored in the ReturnURL querystring value) or if there is no referring page, the user is transferred to default.aspx. The first parameter supplied is the user name and the second is a Boolean value representing whether the authentication cookie should be persisted. If the second value is set to true, the cookie is persisted indefinitely, otherwise, the cookie will timeout after the amount of time you set in the configuration file. If the FormsAuthentication.Authenticate() method or the custom authentication method returns false, you have to let the user know that he or she needs to try again. Else lblResults.Text = "Invalid Username and Password" End If In this code, you set the label text to the message that lets the user know that the user name and password did not authenticate. This is all that is necessary to implement forms authentication on your Web application. Here’s how this authentication works with your application. When users try to access a protected portion of the application, they are redirected to the logon.aspx page. A user logs onto the logon.aspx page and an authentication cookie is created. Depending on whether the cbPersist check box is selected, this cookie is persisted or times out after a set amount of minutes. The user is then returned to the page that he or she originally tried to access. Configuring Passport Authentication The last type of authentication I will cover is Microsoft Passport authentication. Microsoft Passport is a single user name and password with which a user can access sites all over the Internet. This method of authentication has already been adopted by a number of companies, including CitiBank and Monster.com. Although this is a great feature to offer to your visitors, it does require certain licensing that might be too expensive for small sites. In addition, this type of authentication would not be appropriate for an intranet site in which Windows authentication is easily available. The first step to configuring Passport authentication is setting the authentication mode to Passport. The authentication section includes a <passport> element that contains a single property called redirectURL. The redirectURL property is used to point to the page where a user signs in. This URL should point to the Passport logon page that authenticates users for your site. Here is the syntax of the <passport> element. <passport redirectURL="url" /> If an unauthenticated user attempts to access a protected portion of your Web application, he or she will be redirected to the URL specified in the redirectURL property. This is the only configuration setting needed when setting up Passport authentication.
Key Points The authentication section contains a number of very important settings. Here are the key points for working with the section. 40
You can set the mode property of the authentication section to Windows, forms, or passport authentication. You configure the Windows authentication settings with the IIS administration tool. The forms authentication method uses the element to configure various settings for the authentication cookie. You can configure and implement forms authentication on your application with an ASP.NET form and a small bit of .NET code. The passport authentication method uses the <passport> element to configure the location of the passport login page.
Working with the Section After the authentication method verifies a user’s identity, authorization determines whether that user is allowed to access the resource. Authorization can be configured for the server, site, application, sub-directory, or a single page. ASP.NET authorization is based on allowing or denying access based on either the user name or on a role of the user. When a user requests a resource, the ASP.NET process checks if that user has permission to do so before the request is processed. If the user is authorized, the request is processed. If the user is denied, a 401 error is returned. There are two elements that are used in the section: and <deny>. These elements allow or deny access to the resource based on the user name, role, or how the resource is accessed. The ASP.NET process assumes that the user can access the resource, and unless there is a deny rule that applies to the user, the process will grant the user access. Here is the syntax for using the and <deny> elements. <deny users="list of users" roles="list of roles" verbs="list of verbs" /> Both of these elements contain the same properties, which are listed in Table 4-4. Table 4-4: and <deny> properties Property
Description
users
This property is a comma-separated list of users who are either allowed or denied access to the resource. A question mark (?) is used to represent anonymous users and an asterisk (*) is used to represent all users. By placing the domain name in front of the user name. (mydomain\domainuser), you can use this property to authorize domain users as well.
roles
This property is a comma-separated list of roles that are allowed or denied access to this resource. If you are using Windows authentication, Windows groups are used as roles. If you want to use other roles, you will need to create and manage them through your own code.
verbs
This property is a comma-separated list of HTTP access methods 41
Table 4-4: and <deny> properties Property
Description that are allowed or denied to this resource. Available verbs are GET, HEAD, POST, and DEBUG.
Here is an example of how you can deny access to anonymous users and then grant access to a number of different users. <deny users="*" /> In this example, when James or Tammy logs into this site, they will be allowed to access this resource. All other users will be denied. The process will look only for the first rule that applies to the user, and that rule will determine if the user is given access to the resource. In this example, when Tammy tries to access this application, the process will find the first rule and grant her access. If the rules were listed the other way around, she would be denied access because the first rule that applies to her is a deny rule. After the process finds the first rule that applies to the user, it stops reading from the configuration file. Real World
The ability to include domain users in the users property can be very useful when you are building an intranet site. If users are already logging into a domain, and you are using Windows authentication, you can easily set up your application security without any additional user names or passwords. Users simply need to remember to include the domain name followed by a backslash(\) in front of the user name (mydomain\James).
The roles property allows or denies access based on the role of the user. Here is an example of using roles to allow access. <deny users="*" /> In this example, you deny access to all users, and then allow access to any users who are members of the Windows Power Users group. You can use the Windows groups because in this example you are using Windows authentication. If you are not using Windows authentication, you will need to build your own implementation of roles. The other option for allowing and denying access to users is the verb property. The verb property allows or denies access based on how the user is accessing the application, whether the user is accessing it using the GET, POST, HEAD, or DEBUG HTTP method. Here is an example of allowing POST access to the administrator, and denying it to all other users. <deny users="*" verbs="POST" /> 42
In this example, users in the Windows Admins group are allowed to POST to the application; all other users are denied access if attempting to POST to the application. I discussed the location tag in Chapter 2 briefly, but now let’s see how to use the location tag with authorization to control who can access an individual file. In the next example, you will use the location tag to specify the authorization permissions for a single file called admin.aspx. <System.Web> <deny users="*" /> In this example, the only user who would be able to access the admin.aspx file is the user identified as Administrator. Using the location tag is the only way that you can apply authorization permissions to a single file. Real World
Using the location tag comes in very handy when you want to restrict a single directory or single file. You can use the location tag to restrict access to the administration section of your application without having to add a second logon.
You can also apply authorization permissions to a sub-directory of the application. There are two methods of doing this. Use the location tag as in the example just shown and list the name of the directory in the path property. Add a web.config file to the sub-directory and include the authorization permissions in that file.
File Authorization So far, I have discussed request-based authorization, but there is another type of authorization that can be used with ASP.NET. With Windows authentication, if a user attempts to access the .aspx or .asmx file, the ASP.NET process performs an access control list (ACL) check to see if the user has permission to access that file. This means that ASP.NET checks if the user has NTFS permissions to access the file as well as if the user has request-based authorization to access the URL. This provides you with an additional way to restrict users from accessing particular files, but it can be used only when using Windows authentication.
Key Points Authorization is crucial to securing your applications. ASP.NET authorization is based on the URL that the user is accessing and a combination of and <deny> rules in your configuration section. Here are the key points to remember when working with the section.
43
ASP.NET authorization is performed when a user requests a resource. The request is allowed or denied based on the rules contained in the authorization section of the configuration file. You can allow or deny users based on the user name, the user’s roles, or the HTTP method used to access the resource. ASP.NET will use the first rule it finds that applies to the user accessing the resource. ASP.NET assumes that a user has permission to access a resource. The user is denied only if there is a deny rule applied to the requesting user. You can use the location tag to target authorization settings for a directory or even a single file.
Working with the Section ASP.NET uses the section to detect the type of browser that is accessing the application. There are many different browsers, mobile devices, and cell phones that can browse the Web, and each browser can handle a different mix of HTML, script, and other browser features. This section makes it possible for ASP.NET to serve the correct mix of HTML and client-side script for each browser and keep track of what browser can handle which features. Because determining what browser is accessing an application is not what I would call an exact science, this section relies on the HTTP request header and regular expressions to determine what browser or mobile device is accessing the application. When a client requests a file from the server, the client sends an HTTP request including a header. This header contains a number of variables including one that identifies the type of browser accessing the resource, but the name and value of this variable is not exact. ASP.NET reads this value and uses regular expressions to determine the browser and browser version that are accessing the page. This method of detection is not exact, but it is about as close as we are going to get. After ASP.NET determines the type of browser accessing the page, it can customize the HTML output that it sends back to the browser. The information needed to customize the HTML output is also contained in this section, from whether a browser supports frames or tables, to the color depth and screen size of a mobile device. The machine.config file contains a large amount of information in the section. This information includes regular expressions and settings for a large number of browsers and mobile devices. A company called cyScape publishes this information and in the future, you should be able to find updates on their Web site at http://www.cyscape.com/browsercaps. (Currently there are no updates available.) This company also publishes the BrowserHawk browser detection software. However, there have been updates to the mobile controls portion of this section. For more information about the mobile controls updates, please see Chapter 5. The majority of developers do not need to know how to add new browsers or customize settings; they need only to know how to replace the existing information with updated information when it is released. No updates have been released yet, but I assume it will be a pretty simple procedure that does not require much more than cutting and pasting the information into your machine.config file. Some developers will need a deeper understanding of this section, including how the section works and how to add custom browsers or mobile devices to the configuration section.
Understanding Elements There are a number of elements that can be used in the section. The first element in the section is the element. The element specifies the name of the class that holds all the information gathered from the 44
section. Here is the element from the machine.config file on the server. The type property points to the class that holds all the name/value pairs. The class pointed to by the result tag can then be used by the ASP.NET process to read the values and customize the HTML output. The section uses IIS server variables pulled from the HTTP request header. The <use> element defines which server variable will be used as the information source. Here is an example of using the <use> element. <use var="HTTP_USER_AGENT" as="Agent" /> This example specifies that the browser capabilities information will be pulled from the “HTTP_USER_AGENT” IIS server variable. The as property makes the server variable available to the rest of the section. In this example, you would reference the Agent variable in the section, and read the data from the server variable. Later in this chapter, I cover a couple of ways that this is used, but first let’s look at the other element that can be used in the section. The heart of the section is the element. This element matches regular expressions to text found in the server variables and then lists the browser settings that should be included in the browser class. Here is the syntax of the element. name=value name=value The regular expression in the match property is compared to the string in the with property. If there is no with property declared, the string declared in the <use> statement is used. If there is a match between the regex listed in the match property and either the string in the with property or the string declared in the <use> statement, the name/value pairs listed are included in the browser caps class and generate the correct HTML to send to the client. The element includes a sub-element named . The element is used much like the element in that it contains both match and with properties that can match regular expressions to strings. The element is used much like the way a case statement is used in VB. Multiple elements can be included in a element. When a statement is found to match, it will skip the rest of the statements in that group. Here is the syntax for using the element. name=value name=value name=value name=value 45
The first element that finds a match is used and all the other case elements are ignored. This can be a little confusing, so let’s look at an example. <use var="HTTP_USER_AGENT" /> platform=Win95 platform=Win98 platform=WinXP platform=Win2000 This example has no with properties declared, so the elements will search the string that was declared in the last <use> statement. In this case, that string is the HTTP_USER_AGENT server variable. Here is how this element is processed. 1. The first case is processed, and the HTTP_USER_AGENT variable is searched to see if it contains the strings “Windows 95” or “Win95.” 2. If the string contains one of these values, the name/value pair inside the element is added to the browser capabilities class stated in the element. 3. If a match is found, none of the other elements are processed and the element is complete. 4. If a match is not found, the next element is processed and this procedure is repeated. If no match it found, the element is completed without adding any name/value pairs to the class, and the next element begins. In this process, the section handler steps through the section filter by filter and case by case until the browser capabilities class is filled with name/value pairs that describe the browser or device being used to access the resource. To get a better grasp on this, let’s look at a more complex element taken directly from the machine.config file. <use var="HTTP_VIA" as="via"/>
"(?'nokiaVersion'Nokia\D*(?'gatewayMajorVersion'\d+) (?'gatewayMinorVersion'\.\d+)[^,]*)"> gatewayVersion = ${nokiaVersion} gatewayMajorVersion = ${gatewayMajorVersion} gatewayMinorVersion = ${gatewayMinorVersion} This is one of the more complex elements, whose purpose is to detect if the accessing device is a Nokia device. If it is a Nokia device, the element extracts the gatewayVersion, the gatewayMajorVersion, and the gatewayMinorVersion from the server variable. This information is added to the browser capabilities class and can be referenced when tailoring the HTML to be sent back to the browser. There are a couple of conventions used in this statement that were not used in the prior statement; most of these involve the use of regular expressions. I will detail each of them as you go through this statement. The first element in this example is the <use> element. <use var="HTTP_VIA" as="via"/> This element makes the “HTTP_VIA” server variable available to the elements as the via variable. The <use> element is necessary because the information you are looking for is not held in the default HTTP_USER_AGENT server variable, but in the HTTP_VIA variable. Here are the first and elements. The first statement searches the via variable for the Nokia string. If the Nokia string is found in the via variable, the and elements inside the statement will be processed. Here’s a closer look at the and elements included in this statement. gatewayVersion = ${nokiaVersion} gatewayMajorVersion = ${gatewayMajorVersion} gatewayMinorVersion = ${gatewayMinorVersion} The statement in this example matches a number of different variables and makes them available to the browser capabilities class, which will custom tailor the HTML sent to the browser. If you look through the section in the machine.config file, you will find many different and elements that look much like the one just shown. The amount of information here is amazing; it gives ASP.NET the ability to customize HTML for a large number of browsers and mobile devices. 47
Key Points The section makes it possible to customize HTML output for a large number of browsers and mobile devices. You will most likely not need to use this section in your day-to-day development duties, but it is important to know the role this section plays and how this information can be updated in the future to accommodate new browsers or mobile devices. Here are the key points about this section.
The section customizes HTML output based on the browser or mobile device that is accessing the resource. The section uses server variables made available by the HTTP request header to determine what browser or device is accessing the resource. Regular expressions are used to match strings against the request variables to not only determine what browser or device is accessing the resource, but also the version of the browser and its various capabilities. Updates to this information should soon be available from http://www.cyscape.com/browserCaps.
Working with the Section For the majority of the pages you build in ASP.NET, you can rely on the browser detection performed by ASP.NET and the section, but there are times when you’ll want to target or force a page to the level of a particular browser. This is where the section comes into play. The section contains a number of browser aliases that can be used to generate content targeted to a particular browser. Each alias contains a matching userAgent value that represents the targeted browser. The userAgent value is the string that is found in the userAgent server variable when the user accesses the application. This section works much like the or sections in that you can add, remove, or clear out aliases from the section. Here is the default section in the machine.config file. Each alias is added to the collection and linked to a specific userAgent value. The first two aliases target Internet Explorer 4 or 5 level browsers. These targets are very similar, if not the same as using the uplevel browser target. The uplevel alias forces a page to render ASP.NET controls using uplevel settings. The last alias type is the downlevel alias, which forces a page to render ASP.NET controls in downlevel mode.
Uplevel vs. Downlevel There are a number of differences in how ASP.NET controls render, depending on whether clientTarget is set to uplevel or downlevel. A downlevel browser is one that only understands HTML 3.2. If you target a page to the downlevel alias, the page will use only 48
HTML 3.2 when sending HTML to the browser. An uplevel browser or device should be able to handle the following technologies.
ECMAScript version 1.2 (JScript, JavaScript) HTML 4.0 CSS The Microsoft Document Object Model (MSDOM)
When you target a page to uplevel browsers, the page will render using CSS, HTML 4.0, ECMAScript 1.2, and the MSDOM to take full advantage of all the capabilities of the target browser. Examples of uplevel browsers are Internet Explorer 4.0 and above, among others.
Using Client Target in Your Pages It is easy to target your pages to a particular client target alias. Every ASP.NET page has a @page directive at the top of the page, and the @page directive has a property called clientTarget. This property can be set to an alias that is present in the collection, and the page will render based on that alias. Here is an example of using the clientTarget property. <% @Page language="C#" clientTarget="downlevel" %> This page will now render using only downlevel technologies, meaning HTML 3.2. This setting will override any browser detection that is performed by ASP.NET and the section. Real World
An example of the usefulness of the clientTarget property is when working with the TabStrip Web control. When targeting up-level browsers, the tab strip renders using layers, and then switches the layers when a tab is pushed without using a postback. When the control targets a downlevel browser, it sends only one layer to the browser and sends postbacks whenever a tab is pushed. Although the uplevel version of the control is faster, it causes all the tabs to be loaded no matter what tab is currently being viewed. For this reason, I among others have used the clientTarget property to force the tab strip control to render in downlevel mode. This has the unfortunate effect of forcing the entire page to downlevel mode.
Adding and Removing Aliases It’s easy to add and remove aliases from the section using the same elements you use in the section. Here is the syntax for adding aliases to the section. The alias property is referenced from the clientTarget property of the ASP.NET page. The userAgent property replicates the userAgent server variable that would be sent with the browser. If you want to add a new alias based on a specific browser, you capture this userAgent server variable when the browser accesses your site, and then use the userAgent value to add the alias and target the browser in the future. 49
Here is the syntax of how to remove an alias that was defined in an earlier section. Removing an alias from the collection will make that alias unavailable to any pages affected by this configuration file. Just like in the section, you can also use the element to clear out all the aliases previously added to the section. Here is the syntax for the element.
Key Points The section enables you to bypass the default ASP.NET browser detection and target your pages to a particular browser or range of browsers. Here are the key points pertaining to the section. The section works just like the section in that you can use the , , and elements to add, remove, and clear out the collection. You can use the clientTarget property of the @page directive to force a page to render for a particular browser, rather than the browser detected by ASP.NET.
Working with the Section The section includes a number of settings and elements that enable you to configure how ASP.NET is compiled. You can use various elements to add new ASP.NET scripting languages compilers and custom assemblies, and configure various compilation settings. The section has a number of sub-sections, but first we are going to look at the properties that can be set in the element. Here’s the syntax of the element.
Table 4-5 lists the properties for the element.
Table 4-5: properties Property
Description 50
Table 4-5: properties Property
Description
batch
This property controls compilation is supported.
whether
batching
trueBatching is enabled. falseBatching is disabled. batchTimeout
This property specifies the timeout period for batching compilation to complete. If the compilation does not complete in this time frame, the compiler reverts back to single page compilation for just this page.
debug
This property specifies whether debug binaries are compiled. trueDebug is enabled. falseRetail binaries are compiled.
defaultLanguage
This property specifies the default programming language to be used by ASP.NET. This name must match one of the names declared in a sub-tag. The default value is vb.
explicit
This property sets whether the Visual Basic explicit option is enabled. Visual Basic’s explicit option requires that each and every variable is declared using the Dim, Private, Public, or ReDim statement. trueExplicit is enabled. falseExplicit is disabled.
maxBatchSize
This property specifies the maximum number of files that will be included in a batch compilation.
maxBatchGenerated FileSize
This property specifies the maximum size in kilobytes of the generated source file for each batch compilation.
numRecompilesBeforeAppRestart
This property specifies the number of dynamic recompiles that can occur before the entire application is restarted.
tempDirectory
This property specifies the directory to use for temporary file storage during compilation.
strict
This property specifies whether the Visual Basic strict compile option is enabled. The Visual Basic Strict option ensures best programming practices by throwing errors when performing unadvised operations like implicit conversions that could cause data loss. trueStrict is enabled. 51
Table 4-5: properties Property
Description falseStrict is disabled.
Table 4-5 includes a lot of new information, so let’s cover some of it in more detail.
Visual Basic explicitBy default, Visual Basic enables you to use variables that have not been explicitly declared, but this is not a good practice. The Visual Basic Explicit option ensures that each variable is declared using the Dim, ReDim, Private, or Public statement. If you use Visual Basic .NET, I would strongly suggest leave this option enabled in your configuration file. Visual Basic strictBy default, Visual Basic enables some questionable programming operations. When you enable the Strict option, Visual Basic throws an error if either of the following programming procedures are performed. Implicit conversion, which could lead to data loss. Late binding, which is the assigning of a variable that was declared as an object. Batch CompilationPages are compiled when first accessed, which causes a slight delay to the user accessing the page. Batch compilation compiles all the files that have not been compiled in a directory when the first file in that directory is accessed. This means that the first file accessed will be even slower, but there will be no delays at all for subsequent requests.
Here’s a sample section. There are also a number of sub-sections that can be used in the section. The section declares the compiler programs that can be used to compile ASP.NET pages. There are a number of default compilers included with ASP.NET, and you can add compilers to process languages including Delphi. Here’s the syntax of the section. The properties for the section are listed in Table 4-6.
Table 4-6: properties Property
Description
language
This property is a semicolon separated list of language names. These are the names that the language can be referenced by in 52
Table 4-6: properties Property
Description the language property of the @page directive.
extension
This property is the file extension of the code file for this language. Examples are .cs for C# and .vb for Visual Basic.
type
This property is the class that is used to compile files programmed using this language.
warningLevel
This property is the warning level of the compiler.
compilerOptions
This property is a list of options that will be passed to the compiler during compilation.
Here is the entry for the C# language. By listing three different language names, you can use any of those names(c#; cs; csharp) when specifying c# in the @page language property. The element enables you to add a new programming language with which to write ASP.NET.
Adding Assemblies to Compilation If you have ever used one of the .NET command line compilers, you know you need to include the assemblies that you want to use while compiling the application. The section adds the assemblies that should be included when compiling ASP.NET applications. Like the section, this section assembles a collection of the assemblies to use when compiling ASP.NET applications. Here is an example of how to add an assembly to the section. This assembly would be included whenever ASP.NET applications are compiled. You can also remove assemblies using the element. When an assembly is removed from the collection, it is no longer included in the compilation of ASP.NET pages. Again, just like the other collection-based sections, you can use the element. 53
This clears out all the assemblies previously added to the collection.
Key Points The compilation section includes a number of important settings for optimizing the compilation of ASP.NET pages. Here are the key points for working with the section.
The strict and explicit properties of the element enable you to set the Visual Basic strict and explicit settings to true for all your ASP.NET files. Batch compilation can help make the user experience much smoother by compiling all the ASP.NET pages that have not been compiled when the first of those pages is accessed. The element enables you to add languages (even languages like COBOL.NET!) for programming ASP.NET. The section enables you to add assemblies that will be included during the compilation of ASP.NET pages.
Working with the <customErrors> Section The <customErrors> section enables you to control the error messages that users see. This is a very important step in developing a user-friendly application. There are two types of errors that can be sent to users.
HTTP Errors HTTP errors are thrown for a number of reasons including when a page is not found (404) or access is denied to a page (403). Code ErrorsErrors in your code can be caused by a connection to a database failing, an invalid cast, or any number of illegal procedures.
By default, both of these error types produce an “ugly” error message that is sent to a user. This is not only an aesthetic issue but a security issue as well because the error message could include sensitive user name or password information. The <customErrors> element has two properties: the mode property and the defaultRedirect property. Here is the syntax of the <customErrors> element. <customErrors mode="On" defaultRedirect="customerror.aspx"> The mode property determines when the “friendly” error messages should be displayed and when the actual error should be displayed. Table 4-7 lists the possible values for the mode property.
Table 4-7: mode property values Value
Description
On
This value enables custom errors. If no defaultRedirect page is specified, users are presented with a generic error message. A generic message will not reveal the reason for the error, only that an error has occurred. (The error message is big and yellow; I will show 54
Table 4-7: mode property values Value
Description you how to change this as well.)
Off
This value disables custom errors. When disabled, users will see the complete error message whenever an error occurs. Although not desirable when your applications are in production, this information is invaluable during development and debugging stages.
RemoteOnly
This value is the best of both worlds: the RemoteOnly setting presents a complete detailed error message when the application is accessed from the system that hosts it, but when accessed by a different system, only friendlier custom errors are displayed. If you develop on the same machine that hosts your application, this value is very useful because it will not require changes when you move from development to production.
When an application is in development, you want to see detailed error messages so you should set the mode to either Off or RemoteOnly. When the application has been pushed to production, it is essential to set the mode to On or RemoteOnly so that users do not see the detailed error messages. When the mode is set to RemoteOnly or Off, the user sees a generic error message (the big ugly yellow page). It would be nice to be able to customize this generic error message for the application and perhaps include the company logo or technical support instructions. The defaultRedirect property is used to set the generic error message to a page other than the default. This way you can create a custom error message for your application and then set the URL of that page in the defaultRedirect property of the <customErrors> element. Here is an example of the defaultRedirect property. <customErrors mode="On" defaultRedirect="customerror.aspx"> Whenever there is an error in the application, the user will see the customerror.aspx instead of a detailed or generic error message.
Customizing HTTP Error Messages You can further customize this error message by using the <error> element to redirect certain HTTP errors to custom error pages. The <error> element enables you to specify a particular HTTP error code and the page to which users will be redirected. Here is the syntax of the <error> element. <customErrors mode="On" defaultRedirect="customerror.aspx"> <error statusCode="HTTP status code" redirect="httperror.aspx" /> Table 4-8 lists the properties for the <error> element.
Table 4-8: <error> properties Property
Description
statusCode
This property specifies the HTTP code to which this redirection 55
Table 4-8: <error> properties Property
Description will apply. Whenever this type of error occurs, the user will be redirected to the URL specified in the property.
redirect
This property specifies the URL that the user will be redirected.
The <error> element enables you to further customize the error messages for each type of HTTP code. For instance, you might want to have a different error message for a 404 error code vs. a 403 error code. For a 404 error code, you would want to display a message containing a method of searching for the page that could not be found. For a 403 error code, you would most likely want to provide the user with login information or possibly a way for the user to get a password reminder. More Info
You can add custom error handling to your applications using the Application_Error method in the global.asax. This is beyond the scope of this book, but it is an effective method of catching each error produced by an application and sending the user to another page, or generating an e-mail based on that error. You can find some great this procedure at this URL: information on http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnaspnet/html/asp07232001.asp
<customErrors> Key Points The <customErrors> section enables you to customize the error messages that are sent to users.
The mode property of the <customErrors> element enables you to specify the type of errors that are displayed to users. The defaultRedirect property enables you to specify a custom error page where users are sent when an error occurs on your site. The <error> element enables you to customize your error messages even further by specifying custom error pages for specific HTTP error codes.
Working with the Section ASP.NET has the capability to handle requests, responses, and files through a number of encoding methods. The section enables you specify the encoding type and culture for various ASP.NET operations. Here is the syntax for the section. Table 4-9 lists the properties for the section.
Table 4-9: properties Property
Description 56
Table 4-9: properties Property
Description
requestEncoding
This property specifies the expected encoding type for incoming requests including post and querystring data. This value can be overridden by the Accept-Charset value of the request header. By default this value is set to UTF-8.
responseEncoding
This property specifies the type of encoding to use when responding to requests. The default value is UTF-8.
fileEncoding
This property specifies the type of encoding of the various ASP.NET file types including .aspx, .asmx, and .asax.
culture
This property specifies the default culture setting for incoming Web requests. Appendix B lists all the available culture strings.
uiCulture
This property specifies the default culture setting for locale-dependent resource searches.
Here is a sample section. This sets the request, response, and file encoding to UTF-8 and the culture to English. Caution
If you set the fileEncoding property to use UTF-16 encoding and the file is not actually encoded with UTF-16, the output sent to the client could be corrupted and might even include the source code for the page. If you set the fileEncoding property to UTF-16, make sure all of your pages are encoded using UTF-16.
Key Points Globalization plays an important part in developing applications in the multi-cultural Internet world of today.
You can set the type of encoding for requests, responses, and ASP.NET files using the properties of the element. You can set the culture for Web requests using the culture property of the element.
Working with the Section HTTP handlers are classes that process HTTP requests for a particular file extension, much like ISAPI extensions. HTTP handlers exist for .aspx, .asmx, .rem and many of the other default ASP.NET file extensions. The section maps HTTP requests to the correct HTTP handler based on the HTTP verb, the file extension, and the URL of the resource. The section is yet another collection-based section, which means that you can add, remove, and clear out entries in the section. Here is the syntax for adding a HTTP handler mapping to the section. 57
Table 4-10 lists the properties of the section.
Table 4-10: properties Property
Description
Verb
This property is a comma-separated list of the HTTP verbs that this mapping should apply to. You can also use a wildcard (*) to map all HTTP verbs to the handler.
Path
This property is the location where you specify the URL that this mapping should apply to. Generally, the path is a wildcard that includes all files of a particular extension, for instance, *.aspx.
type
This property is the .NET class/assembly that is the HTTP handler.
validate
This property is a value that specifies when ASP.NET loads the HTTP handler class. When set to false, ASP.NET does not load the HTTP handler class until a request comes that fits the criteria listed in the request mapping. When set to true, the class will be loaded when the configuration file is first processed.
Here is a sample of the .aspx handler mapping.