Hands-on Custom Providers: The Contoso Times
Introduction to the Provider Model
Site Map Providers
Session State Providers
Web Event Providers
Web Parts Personalization Providers
Custom Provider-Based Services
Hands-on Custom Providers: The Contoso Times
Click here to download the code sample for this article.
Included with this whitepaper is a sample ASP.NET 2.0 application named Contoso Times (henceforth referred to as "Contoso") that models a newspaper-style content site and provides a hands-on medium for running several of the sample providers presented in this document. Specifically, Contoso is capable of using the following custom providers:
The sections that follow provide instructions for getting Contoso up and running —first using providers included with ASP.NET 2.0, and then using the custom providers listed above.
Perform the following steps to install Contoso on your Web server and configure it to use ASP.NET's SqlMembershipProvider, SqlRoleProvider, XmlSiteMapProvider, SqlProfileProvider, and EventLogWebEventProvider providers:
- Create a directory named Contoso on your Web server.
- Copy the Contoso files into the Contoso directory. These files include a configuration file named Custom.config that makes the providers listed above the default providers.
- Ensure that Microsoft SQL Server is installed on your Web server. Then use the installation script in the supplied Contoso.sql file to create the Contoso database.
- If you want ASP.NET's SQL providers to store state in SQL Server, use the Aspnet_regsql.exe utility that comes with ASP.NET 2.0 to create the Aspnetdb database. If you'd prefer that the SQL providers use SQL Server Express instead, delete the following statements from Web.config and Custom.config:
<remove name="LocalSqlServer"/> <add name="LocalSqlServer" connectionString="Server=localhost;Integrated Security=True; Database=aspnetdb;Persist Security Info=True" />
- Open the Contoso site in Visual Studio 2005 and use the Website->ASP.NET Configuration command to run the Web Site Administration Tool.
- Click the Web Site Administration Tool's Security tab. On the Security page, use the "Create or Manage Roles" link to create roles named "Members" and "Administrators."
- Go back to the Security page and click "Create Users." Create a pair of users named Bob and Alice. Add Bob to the "Members" role, and Alice to both the "Members" and "Administrators" roles.
- Close the Web Site Administration Tool and return to Visual Studio.
- Use Visual Studio's Debug->Start Without Debugging command to launch Contoso. You should see the home page depicted in Figure 1.
Figure 1. The Contoso Times home page
Now that the application is installed and configured to use the default providers, you can take it for a test drive. Here's how:
- Click the "Login" link and log in as Bob. After you're redirected back to the home page, the text to the right of "Members Only" in the Members Only box at the bottom of the page should read "Welcome back, Bob," indicating that you're logged in as Bob and that SqlMembershipProvider is providing data to the membership service.
- Log out by clicking the "Logout" link. Then log in again, but this time log in as "Alice." Observe that two new links appear to the left of the "Logout" link: "Admin" and "Recent Items." When you were logged in as Bob, the "Recent Items" link appeared but the "Admin" link did not. In Contoso, anonymous users see one link, users who belong to the "Members" role but not the "Administrators" role see two links, and users who belong to the "Administrators" role see three links. SqlRoleProvider is providing role data to the ASP.NET role manager. The appearing and disappearing links are managed by a LoginView control, which uses role memberships and login status to display content differently to different users.
- The navigation bar on the left side of the home page is a TreeView control that obtains its data from a SiteMapDataSource. The SiteMapDataSource, in turn, gets its data from XmlSiteMapProvider, which reads the site map from Web.sitemap. XmlSiteMapProvider is currently configured to enable security trimming, which explains why anonymous users see two sets of links in the navigation bar, but logged-in users see three.
- Open the Windows event log and observe the entry created there when the application started up. Logging occurred because of the <healthMonitoring> element in Web.config that maps to Application Lifetime events to EventLogWebEventProvider.
- While logged in as Bob or Alice, use the links on the home page to view a few articles. Then click the "Recent Items" link to see a list of the articles you've viewed the most recently. Information regarding recently viewed articles is stored in the user profile, which is currently managed by SqlProfileProvider. Inspect the <profile> section of Web.config to see how the profile is defined.
Feel free to explore other parts of Contoso as well. For example, clicking the "Admin" link that's visible to administrators takes you to Contoso's secure back-end, which uses GridView and DetailsView controls to provide robust content-editing capabilities.
Now let's reconfigure Contoso to run with the custom providers listed at the beginning of this chapter. First rename Web.config to something else (for example, x-Web.config). Then rename Custom.config to Web.config. Custom.config contains configuration elements replacing SqlMembershipProvider with ReadOnlyXmlMembershipProvider, SqlRoleProvider with ReadOnlyXmlRoleProvider, XmlSiteMapProvider with SqlSiteMapProvider, SqlProfileProvider with TextFileProfileProvider, and EventLogWebEventProvider with TextFileWebEventProvider. Membership and role data now come from the file named Users.xml (where Bob and Alice are assigned the password "contoso!"); the site map now comes from the SiteMap table in the SQL Server database that contains Contoso's article content; profile data is now stored in text files in the ~/App_Data/Profile_Data directory; and Application Lifetime Web events are logged in ~/App_Data/Contosolog.txt.
If you exercise the application using the steps prescribed in Running Contoso with Default Providers, you'll find that it works exactly as before. To the user, the application looks no different. But on the inside, it now relies on custom data sources serviced by custom providers —a great example of the provider model at work, and of the transparency it lends to ASP.NET 2.0 state management.