Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1495 Chapter 32: Instrumentation Using the TemplatedMailWebEventProvider Another option for e-mailing Web events is to use the TemplatedMailWebEventProvider object. This works basically the same as the SimpleMailWebEventProvider ,butthe TemplatedMailWebEvent Provider allows you to create more handsome e-mails (they might get noticed more). As with the other providers, you use the <add /> element within the <providers> section to add this provider. This process is illustrated in Listing 32-13. Listing 32-13: Adding a TemplatedMailWebEventProvider <add name="TemplatedMailProvider" type="System.Web.Management.TemplatedMailWebEventProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" template=" /MailTemplates/ErrorNotification.aspx" from="website@company.com" to="admin@company.com" cc="adminLevel2@company.com" bcc="director@company.com" subjectPrefix="Action required." buffer="false" detailedTemplateErrors="true" maxMessagesPerNotification="1" /> After the provider is added, you also need to add a rule that uses this provider in some fashion (as you do with all the other providers). You add it by referencing the TemplatedMailWebEventProvider name within the provider attribute of the <add /> element contained in the <rules> section. Be sure to set up the <smtp> section, just as you did with the SimpleMailWebEventProvider . After these items are in place, the next step is to create an ErrorNotification.aspx page. This page construction is illustrated in Listing 32-14. Listing 32-14: Creating the ErrorNotification.aspx page VB <%@ Page Language="VB" %> <%@ Import Namespace="System.Web.Management" %> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim meni As MailEventNotificationInfo = _ TemplatedMailWebEventProvider.CurrentNotification Label1.Text = "Events Discarded By Buffer: " & _ meni.EventsDiscardedByBuffer.ToString() Label2.Text = "Events Discarded Due To Message Limit: " & _ meni.EventsDiscardedDueToMessageLimit.ToString() Label3.Text = "Events In Buffer: " & meni.EventsInBuffer.ToString() Label4.Text = "Events In Notification: " & _ meni.EventsInNotification.ToString() Label5.Text = "Events Remaining: " & meni.EventsRemaining.ToString() Label6.Text = "Last Notification UTC: " & _ meni.LastNotificationUtc.ToString() Label7.Text = "Number of Messages In Notification: " & _ Continued 1495 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1496 Chapter 32: Instrumentation meni.MessagesInNotification.ToString() DetailsView1.DataSource = meni.Events DetailsView1.DataBind() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body> <form id="form1" runat="server"> <asp:label id="Label1" runat="server"></asp:label><br /> <asp:label id="Label2" runat="server"></asp:label><br /> <asp:label id="Label3" runat="server"></asp:label><br /> <asp:label id="Label4" runat="server"></asp:label><br /> <asp:label id="Label5" runat="server"></asp:label><br /> <asp:label id="Label6" runat="server"></asp:label><br /> <asp:label id="Label7" runat="server"></asp:label><br /> <br /> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal"> <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" /> <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" /> <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" /> <AlternatingRowStyle BackColor="#F7F7F7" /> </asp:DetailsView> </form> </body> </html> C# <%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Management" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { MailEventNotificationInfo meni = TemplatedMailWebEventProvider.CurrentNotification; Label1.Text = "Events Discarded By Buffer: " + meni.EventsDiscardedByBuffer.ToString(); Label2.Text = "Events Discarded Due To Message Limit: " + meni.EventsDiscardedDueToMessageLimit.ToString(); Label3.Text = "Events In Buffer: " + meni.EventsInBuffer.ToString(); Label4.Text = "Events In Notification: " + meni.EventsInNotification.ToString(); Continued 1496 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1497 Chapter 32: Instrumentation Label5.Text = "Events Remaining: " + meni.EventsRemaining.ToString(); Label6.Text = "Last Notification UTC: " + meni.LastNotificationUtc.ToString(); Label7.Text = "Number of Messages In Notification: " + meni.MessagesInNotification.ToString(); DetailsView1.DataSource = meni.Events; DetailsView1.DataBind(); } </script> To work with the TemplatedMailWebEventProvider ,youfirstimportthe System.Web.Management namespace. This is done so you can work with the MailEventNotificationInfo and TemplatedMail- WebEventProvider objects. You first create an instance of the MailEventNotificationInfo object and assign it a value of the TemplatedMailWebEventProvider.CurrentNotfication property. Now, you have access to an entire series of values from the Web event that was monitored. This e-mail message is displayed in Figure 32-15. Figure 32-15 As you can see in this figure, the e-mail message is more readable in this format. 1497 Evjen c32.tex V2 - 01/28/2008 4:05pm Page 1498 Chapter 32: Instrumentation Summary Whereas ASP.NET 1.x was really focused on the developer, ASP.NET 2.0 and 3.5 have made tremendous inroads into making life easier for the administrator of the deployed ASP.NET application. In addition to a number of GUI-based management and administration tools (covered in the next chapter), you can now record and send notifications about the health (good or bad) of your ASP.NET applications using the ASP.NET health monitoring capabilities. 1498 Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1499 Administration and Management You have almost reached the end of this book; you have been introduced to ASP.NET 3.5 with its wonderful features designed to help you become a better and more efficient programmer. However, with all advancement comes complexity, as is the case in the areas of ASP.NET configuration and management. The good news is that the ASP.NET development team realized this and provided tools and APIs that enable developers to configure and manage ASP.NET–based applications with reliability and comfort. This chapter covers these tools in great detail in an e ffort to educate you about some of the options available to you. This chapter explores two powerful configuration tools: the ASP.NET Web Site Administration Tool, a Web-based application, and the IIS Manager, which is used to configure your ASP.NET applications. The ASP.NET Web Site Administration Tool When ASP.NET was first released, it introduced the concept of an X ML-based configuration file for its Web applications. This web.config file is located in the same directory as the application itself. It is used to store a number of configuration settings, some of which can override configuration settings defined in machine.config file or in the root server’s web.config file. Versions of ASP.NET before ASP.NET 2.0, however, did not provide an administration tool to make it easy to configure the settings. Because of this, a large number of developers around the world ended up creating their own configuration tools to avoid having to work with the XML file manually. The ASP.NET Web Site Administration Tool enables you to manage Web site configuration through a simple, easy-to-use Web interface. It eliminates the need for manually editing the web.config file. If no web.config file exists when you use the administration tool for the first time, it creates one. By default, the ASP.NET Web Site Administration Tool also creates the standard ASPNETDB.MDF SQL Server Express Edition file in the App_Data folder of your Web site to store application data. Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1500 Chapter 33: Administration and Management The changes made to most settings in the ASP.NET Web Site Administration Tool take effect immediately. You find tm reflected in the web.config file. The default settings are automatically inherited from any configuration files that exist in t he root folder of a Web server. The ASP.NET Web Site Administration Tool enables you to create or update your own settings for your Web application. You can also override the settings inherited from uplevel configuration files, if an override for those settings is allowed. If overriding is not permitted, the setting appears dimmed in the administration tool. The ASP.NET Web Site Administration Tool is automatically installed during installation of the .NET Framework version 3.5. To use the administration tool to administer your own Web site, you must be logged in as a registered user of your site and you must have read and write permissions to web.config . You cannot access the ASP.NET Web Site Administration Tool remotely or even locally through IIS. Instead, you access it with Visual Studio 2008, which, in turn, uses its integrated web server (formally named Cassini) to access the administration tool. In order t o access this tool through Visual Studio 2008, open the website and click t he ASP.NET Configuration button found in the menu located at the top of the Solution Explorer pane. Another way to launch this tool is to select ASP.NET Configuration from the Website option in the main Visual Studio menu. Figure 33-1 shows the ASP.NET Web Site Administration Tool’s welcome page. Figure 33-1 The ASP.NET Web Site Administration Tool features a tabbed interface that groups related con- figuration settings. The tabs and the configuration settings that they manage are described in the following sections. 1500 Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1501 Chapter 33: Administration and Management The Home Tab The Home tab (shown previously in Figure 33-1) is a summary that supplies some basic informa- tion about the application you are monitoring or modifying. It provides the name of the application and the current user context in which you are accessing the application. In addition, you see links to the other administration tool tabs that provide you with summaries of their settings. To make any changes to your Web application, you simply click the appropriate tab or link. Remember that most changes to configuration settings made using this administration tool take effect immediately, causing the Web application to be restarted and currently active sessions to be lost if you are using an InProc session. The best practice for administrating ASP.NET is to make configuration changes to a development version of your application and later publish these changes to your production application. That’s why this tool can’t be used outside of Visual Studio. Some settings (those in which the administration tool interface has a dedicated Save button) do not save automatically. You can lose the information typed in these windows if you do not click the Save button to propagate the changes you made to the web.config file. The ASP.NET Web Site Administration Tool also times out after a period of inactivity. Any settings that do not take effect immediately and are not saved will be lost if this occurs. As extensive as the ASP.NET Web Site Administration Tool is, it manages only some of the config- uration settings that are available for your Web application. All other settings require modification of configuration files manually, by using the Microsoft Management Console (MMC) snap-in for ASP.NET if you are using Windows XP, using the Internet Information Services (IIS) Manager if you are using Windows Vista, or by using the Configuration API. The Security Tab Use the Security tab to manage access permissions to secure sections of your Web application, user accounts, and roles. From this tab, you can select whether your Web application is accessed on an intranet or from the Internet. If you specify the intranet, Windows-based authentication is used; otherwise, forms-based authentication is configured. The latter mechanism relies on you to manage users in a custom data store, such as SQL Server database tables. The Windows-based authentication employs the user’s Windows logon for identification. User information is stored in a SQL Server Express database by default ( ASPNETDB.MDF ). The database is automatically created in the App_Data folder of the Web application. It is recommended that you store such sensitive information on a different and more secure database, perhaps located on a separate server. Changing the data store might mean that you also need to change the under- lying data provider. To accomplish this, you simply use the Provider tab to select a different data provider. The Provider tab is covered later in this chapter. You can configure security settings on this tab in two ways: select the Setup Wizard, or simply use the links provided for the Users, Roles, and Access Management sections. Figure 33-2 shows the Security tab. You can use the wizard to configure initial settings. Later, you learn other ways to create and modify security settings. 1501 Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1502 Chapter 33: Administration and Management Figure 33-2 The Security Setup Wizard The Security Setup Wizard provides a seven-step process ranging from selecting the way the user will be authenticated to selecting a data source for storing user information. This is followed by definitions of roles, users, and access rules. Be sure to create all folders that need special permissions before you engage the wizard. Follow these steps to use the Security Setup Wizard: 1. The wizard welcome screen (shown in Figure 33-3) is informational only. It educates you on the basics of security management in ASP.NET. When you finish reading the screen, click Next. 2. Select your access method (authentication mechanism). You have two options: ❑ From the Internet: Indicates you want forms-based authentication. You must use your own database of user information. This option works well in scenarios where non-em- ployees need to access the Web application. ❑ From a Local Area Network: Indicates users of this application are already authen- ticated on the domain. You do not have to use your own user information database. Instead, you can use the Windows web server domain user information. Figure 33-4 shows the screen for Step 2 of the process. 1502 Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1503 Chapter 33: Administration and Management Figure 33-3 Select From the Internet, and click the Next button. 3. Select Access Method. As mentioned earlier, the ASP.NET Web Site Administration Tool uses SQL Server Express Edition by default. You can configure additional providers on the Providers tab. In the Step 3 screen shown in Figure 33-5, only an advanced provider is dis- played because no other providers have b een configured yet. Click Next. 4. Define Roles. If you are happy with all users having the same access permission, you can simply skip this step by deselecting the Enable Roles for This Web S ite check box. If this box is not checked, clicking the Next button takes you directly to the User Management screens. Check this box to see how to define roles using this wizard. The screen from Step 4 is shown in Figure 33-6. When you are ready, click Next. The next screen (see Figure 33-7) in the wizard enables you to create and delete roles. The roles simply define categories of users. Later, you can provide users and access rules based on these roles. Go ahead and create roles for Administrator, Human Resources, Sales, and Viewer. Click Next. 5. Add New Users. Earlier, you selected the From the Internet option, so the wizard assumes that you want to use forms authentication and provides you with the option of creating and managing users. The From a Local Area Network option, remember, uses Windows-based authentication. 1503 Evjen c33.tex V2 - 01/28/2008 4:18pm Page 1504 Chapter 33: Administration and Management Figure 33-4 Figure 33-5 1504 . server domain user information. Figure 33 -4 shows the screen for Step 2 of the process. 150 2 Evjen c 33. tex V2 - 01/28/2008 4:18pm Page 15 03 Chapter 33 : Administration and Management Figure 33 -3 Select. select ASP. NET Configuration from the Website option in the main Visual Studio menu. Figure 33 -1 shows the ASP. NET Web Site Administration Tool’s welcome page. Figure 33 -1 The ASP. NET Web Site Administration. Instrumentation Summary Whereas ASP. NET 1.x was really focused on the developer, ASP. NET 2.0 and 3. 5 have made tremendous inroads into making life easier for the administrator of the deployed ASP. NET application. In addition to