Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning ASP.NET 2.0 With CSharp (2006) [eng]

.pdf
Скачиваний:
83
Добавлен:
16.08.2013
Размер:
20.33 Mб
Скачать

Chapter 11

Figure 11-20

5.Enter the following code into the new Class file:

using System; using System.Web;

using System.Web.UI;

namespace Wrox.Web.GlobalEvents

{

public class ThemeModule : IHttpModule

{

public void Init(HttpApplication context)

{

context.PreRequestHandlerExecute +=

new EventHandler(this.app_PreRequestHandlerExecute);

}

private void app_PreRequestHandlerExecute(object Sender, EventArgs E)

{

Page p = HttpContext.Current.Handler as Page; if (p != null)

{

ProfileCommon pb = (ProfileCommon)HttpContext.Current.Profile; p.Theme = pb.Theme;

}

}

public void Dispose()

{

}

}

}

6.Save the file. Open Web.config and add the following highlighted code, just below the

System.Web node:

418

Roles and Profiles

<system.web>

<httpModules>

<add name=”Page” type=”Wrox.Web.GlobalEvents.ThemeModule”/> </httpModules>

7.Run the page. You will see that there is now a drop-down box (shown at the bottom of Figure 11-21) on the page that enables you to select different themes to store in your user profile.

Figure 11-21

How It Works

In this example, you extended the basic FanClub.aspx page so that users can store their preferred theme in their profile. This facility is available only to registered members of the Fan Club.

Amending the layout code was a simple process of adding a DropDownList control to the page:

<asp:DropDownList ID=”ThemeList” runat=”server”> <asp:ListItem Text=”Default” Value=”” /> <asp:ListItem Text=”Wrox Red” Value=”WroxRed” /> <asp:ListItem Text=”Wrox Blue” Value=”WroxBlue” />

</asp:DropDownList>

419

Chapter 11

The value for the theme is stored as a simple string of text in the user’s profile, and is set in the same way as any other profile setting:

Profile.Theme =

((DropDownList)FCLoginView.FindControl(“ThemeList”)).SelectedValue;

This code means “find me the ThemeList control; it’s a DropDownList, so get me its SelectedValue property.” In this case, the DropDownList control’s SelectedValue property will be whatever the Value property for the selected item is set to, so, for example, if the user selected the item with the text “Wrox Red,” the value stored would be WroxRed.

At this stage, the profile has been updated with the user’s preference, but the last bit of this exercise gave the site the capability to apply the selected theme to each page. You used an HttpModule to do this.

An HttpModule contains code that is processed every time any page in an application is processed. The module can be enabled and disabled via the Web.config file.

The module code can handle events that are raised each time a page is requested.

In this case, the event that is intercepted is processed before the code on a page is processed, and the requested page’s theme can be changed to match the theme specified in the user’s profile.

This section of the code is getting to be quite advanced in terms of programming techniques. Don’t worry if you don’t fully understand how this works — you will encounter more code like this in Professional ASP.NET 2.0, so you can treat this as a taste of what you can expect when you start to unleash the full power of ASP.NET.

Take a look at the code in the HttpModule and see how it works:

using System; using System.Web;

using System.Web.UI;

namespace Wrox.Web.GlobalEvents

{

public class ThemeModule : IHttpModule

{

The first part of the code in the file imports a few useful namespaces that contain classes you’ll need later in the code. The ThemeModule class is configured to reside within the Wrox.Web.GlobalEvents namespace. Additionally, the ThemeModule class implements the IHttpModule interface, which turns this simple class into a class that can be used as an HttpModule, provided you then implement the required methods, because there are some specific methods that all HttpModules must implement in order to work correctly.

In this case, two methods have to be implemented: the Init() method and the Dispose() method. Here’s the Dispose() method — because there’s nothing that you need to do in this method, the empty method signature will just sit there in the file not bothering anyone:

420

Roles and Profiles

public void Dispose()

{

}

Now compare that to the code in the Init() method:

public void Init(HttpApplication context)

{

context.PreRequestHandlerExecute +=

new EventHandler(this.app_PreRequestHandlerExecute);

}

The code within the method itself is a bit tricky to grasp at first. There’s only one line to consider, which declares a new event handler. The event handler will run each time the current context is at the PreRequestHandlerExecute stage in the request life cycle — in plain English, that’s pretty early in the request stage. This line of code means that each time the appropriate event fires, you can add an event handler that will be processed:

private void app_PreRequestHandlerExecute(object Sender, EventArgs E)

{

Page p = HttpContext.Current.Handler as Page; if (p != null)

{

ProfileCommon pb = (ProfileCommon)HttpContext.Current.Profile; p.Theme = pb.Theme;

}

}

This method handles that PreRequestHandlerExecute event. The first line in this method gets the currently requested page object so that you can interact with it:

Page p = HttpContext.Current.Handler as Page;

Notice the method used here to cast the current Handler object to a Page object type. This means that if the requested item is a page, the cast will be successful and the p object will contain an object reference to the requested page. However, if the item requested is not a page, the cast fails, and the p object is set to NULL.

As long as the Page object exists (and is not set to Nothing), you can then try to apply the theme to the page by obtaining the current profile properties and using that profile’s Theme property:

if (p != null)

{

ProfileCommon pb = (ProfileCommon)HttpContext.Current.Profile; p.Theme = pb.Theme;

}

Notice that the way casting is performed in this case (casting the current Profile object to type Profile Common) will throw an exception if the attempted cast fails. The only case when this would happen would be if you disabled profiles in the Web.config file, which would surely make this example a bit pointless. At the end of this method, the Theme property of the page is set to the value stored in the profile property called Theme.

421

Chapter 11

Finally, there was a new section added to the Web.config file that enabled the HttpModule itself:

<system.web>

<httpModules>

<add name=”Page” type=”Wrox.Web.GlobalEvents.ThemeModule”/> </httpModules>

The <httpModule> section can be filled with many different modules that you may want to run. In this example, there is just one module — the ThemeModule. The ThemeModule class that was declared is added in the type attribute, along with the namespace that it resides in, so that .NET knows exactly which ThemeModule you want to run.

That’s about it for this example. It’s a tricky piece of code, but it’s a neat way to make themes switch instantly on pages, and to stay switched for all pages on a site, because the module will run for each requested page on a site.

Managing Anonymous Shopping Car ts

Earlier this chapter, the presence of the following profile property was briefly mentioned:

<profile enabled=”true”> <properties>

<add name=”MemberName”/>

...

<add name=”Cart” serializeAs=”Binary” type=”Wrox.Commerce.ShoppingCart”

allowAnonymous=”true”/>

The allowAnonymous flag that is stored against the Profile.Cart value for each user session indicates that anonymous users can have a shopping cart. In this case, users may visit the site and neglect to log on, or they may be completely new to the site. The user will then happily fill up a basket with items, so it will be up to you to handle either switching the cart to an existing user when they log in, or creating a new profile when the anonymous user logs in and transferring the cart to the newly created user when the user attempts to check out.

There is code that handles this transition in the Global.asax file, which handles global events raised by the code. Here’s the code as it is used in the Wrox United application:

void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs e)

{

// get the profile for the anonymous user

ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);

// if they have a shopping cart, then migrate that to the authenticated user if (anonProfile.Cart != null)

{

if (Profile.Cart == null)

Profile.Cart = new Wrox.Commerce.ShoppingCart();

Profile.Cart.Items.AddRange(anonProfile.Cart.Items);

anonProfile.Cart = null;

422

Roles and Profiles

}

ProfileManager.DeleteProfile(e.AnonymousID);

AnonymousIdentificationModule.ClearAnonymousIdentifier();

}

This code is examined in far more detail in Chapter 13, when you see exactly how the whole shopping experience works, but it’s useful to understand that there are consequences when you log in as a user. The transferring of a shopping cart from one profile to another is the most important, because none of the other properties can actually be set for anonymous users.

Summar y

This chapter delved deeper into the world of roles, and introduced the concept of user profiles for storing additional data about a user. The two technologies can sit happily side-by-side, and used together are a great way to control access to a site and for customizing the user experience.

In this chapter, you had a chance to take more control over the access permitted to users on a fully functional web application, including the following:

Show or hide resources to selected user roles.

Display different page content depending on which role views the page.

Display links to selected pages, but prompt the user for more credentials for authorization.

Store more information about users in their user profile.

Store different types of data in a user profile, including the user’s preference for site theme.

These techniques will be expanded in the upcoming chapters to form part of the shopping cart functionality built in to the Wrox United site.

Exercises

1.Create a new user account called Admin, and make this user a member of all groups. Log in to the Wrox United site as Admin and test to see whether you can access all of the different pages in both the fan club and the administration section.

2.Remove the Admin user from any role other than the Administrator role and test the site again. Ensure that you can access the following pages:

Admin.aspx

EditNews.aspx

UpdateProducts.aspx

3.The Admin user has had a request by a reporter out in the field — he’s uploaded a score for a match, but the score has just changed. Unfortunately, the reporter’s laptop has run out of battery, so he’s asked the Admin user to update the score for him. Make sure that the Admin user can update the score of a match.

423

Chapter 11

4.Add a field to the user profile for the Wrox United application called DateOfBirth. Give this property a data type of System.DateTime. Add an input field to the FanClub.aspx page so that users can update their date of birth. Note that you will need to convert the value entered in a text box to a DateTime value when you save the data — you might want to try the DateTime

.Parse() function for this purpose. To retrieve the data, use the Profile.DateOfBirth

.ToShortDateString() function.

424

12

Web Ser vices

Web services have been “the next great thing” for a little too long now. They were originally introduced as part of the .NET Framework in its first incarnation (although they have been present in Java for a lot longer). Web services are a method of making information available in a standardized way that could be accessed by any developer’s application over the Web. Web services can form a library or shared repository of information that could be anything from the latest weather forecast, a map of your local amenities, a mathematical function calculator, to the tracks and album cover of the CD that you just inserted into your CD-ROM drive. You should be aware, though, that a web service on its own isn’t an application. Web services aren’t rendered as web pages, or as executable files (.exe); they come completely shorn of a user interface. It’s up to you as a developer to use them and integrate them into your applications. They are there to save you time and effort by reducing code duplication.

They can be used in one of two ways. You can create a web service that is exposed to the web, to share with other developers and other applications. Or you can search for a web service that can be added to your own application. (They are similar to plug-ins in that respect.) For example, if your application is a company web site and you want to include directions to company headquarters, rather than scan in a map, or attempt to hand-draw one in a graphics tool, why not use one of the web services that makes sections of maps available online?

The information contained in the web service is wrapped up as an XML document (in other words, plain text), so that worries about platform-specific concerns, such as whether you’re accessing them on an old Windows 98 machine or a turbo-powered Powerbook, simply evaporate. Web services aren’t really anything new either — components have been performing a similar trick for many years now. However, there are some crucial differences. Components had to be distributed by the developer, and they had to be downloaded and installed. These things alone meant that they tended to be tied to one platform. This isn’t true of web services. A key word that emphasizes what web services offer is standard. Everything to do with web services is standardized: the method of transmission, the method used to wrap the web service up, the way the web service is defined, all have clear W3C standards associated with the technologies involved. And to make life much easier, all these standards are based on XML. So they’re quick and easy to download, and even easier to use.

Chapter 12

This chapter looks at the following:

Introducing web services

Consuming web services

What happens in the typical life cycle of a web service

Testing a web service

Discovering a web service

Creating and consuming an example web service that uses a parameter

Creating a proxy — the old way of doing things

Web service security

Looking at Web Services

The introduction alluded to the fact that web services haven’t quite taken off in the way that was expected, and it’s true and there are probably several reasons for that, but none that should stop you from using them. One is that developers and businesses don’t like giving away their hard work for free. The truth is when businesses create a web service, it usually isn’t for public consumption. It is for inhouse use only, and even if it is available for the public to use, it is likely to come at a price. On the flipside, with free web services, you have no guarantee that they will still be there in a week or a year’s time, so it’s no good basing a large application around them. So do you fork out for a web service and increase the cost of your application, or take a gamble that a particular service will still be in existence over the next few years? This dilemma isn’t easily remedied.

Second is lack of education. People are still largely unaware of what web services can offer. I recently worked on a customer services application, which had to integrate with an existing contacts manager application and detect when the contacts manager was running and if it had loaded the latest customer information correctly. Rather than use a web service to make this information available, the client chose instead to write to the clipboard (especially awkward considering the users themselves might also be using the clipboard) and then my application had to periodically check the clipboard for the text to indicate that the contacts manager was running. As a result, the application was 20 times more complicated than it needed to be.

Third is the lack of a single killer application. Before you started this chapter, could you name five common examples of web services? The problem is that they’re too good at the job they do, so they become practically invisible. Where is the Google or Internet Explorer of web services? I don’t know, but you might just be using it unwittingly. A good web service should dovetail seamlessly into your application, and your user should never know it’s there.

Last is the apparent learning curve required. With web services, it’s easy to get drawn into listing the mass of technologies involved, and therefore easy to get confused about what you need to know. SOAP, WSDL, UDDI, and DISCO all play valid parts in the process of making a web service available. However, you don’t need to know about any of these acronyms to be able to use a web service.

In fact, before you start creating a web service and learning what goes into them, it’s a good idea to look at an example first.

426

Web Services

Consuming a Third-Party Web Service

Consuming a web service is almost as easy as firing up your browser, typing in a URL of a web service, and pressing Return. Perhaps we’re oversimplifying a little bit here, but if you browse to the URL of a web service endpoint, you will find that you can use it functionally, albeit without any graphical frills. An endpoint is the specific location where the web service can be accessed. So let’s do exactly that. Because of the nature of the Web, sites are very often up and down. If you wanted to phone up a plumber on the other side of the city, you’d check your local phone directory for a number — but that plumber may or may not still be in business, and web services are just the same. Several good starting points are the following sites: www.webservicex.net, www.webservicelist.com, www.webservice oftheday.com, and www.wsiam.com. All of these sites provide lists of web services you can browse. More alternatives are available, and if one of these sites is inoperable, another will be working.

There are plenty of possibilities to integrate web services into your example site. Wrox United chronicles the football fixtures, results, merchandise, and news of one soccer team. Now Wrox United isn’t always the most successful soccer team in the league, and they’re usually looking for an excuse on which to hang a particularly poor performance, such as the weather. It would be nice if you could stick a weather report on the bottom of your match reports, just so you know that if the goalkeeper claims the fifth and sixth goals slipped through his hands, maybe the 85-degree temperature would be a bit of a giveaway.

In the following Try It Out, you start by finding an example weather web service that is freely available.

Try It Out

Consuming an Example Web Service

1.To be able to access a web service, you need to locate an .asmx file (you’ll learn what one of those is after you’ve finished this example). Figure 12-1 shows an example of a weather service at www.webservicex.net/globalweather.asmx.

Figure 12-1

If for any reason this web service is giving an HTTP error 404, go back to any of the previous web service directories, type Weather into the search link, and use one of the example sites it returns. Most weather web services work the same way.

427