03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Introduc<strong>in</strong>g the Log<strong>in</strong> Controls x 607<br />

The beauty of the provider model is that you can swap providers through configuration without any<br />

changes to your programm<strong>in</strong>g code. If you have your own custom data store, you could write your own<br />

provider <strong>and</strong> plug it <strong>in</strong>to your website to replace one of the default providers with very little effort.<br />

Each provider needs a data store — represented by the bottom part of the diagram <strong>in</strong> Figure<br />

16-1 — <strong>and</strong> is written to work with one or more specific data stores. For example, the<br />

DefaultMembershipProvider (to h<strong>and</strong>le membership services such as creat<strong>in</strong>g users, logg<strong>in</strong>g <strong>in</strong>,<br />

<strong>and</strong> resett<strong>in</strong>g passwords) <strong>and</strong> the DefaultRoleProvider (to h<strong>and</strong>le role-related tasks) are designed<br />

to work with any version of Microsoft SQL Server, <strong>in</strong>clud<strong>in</strong>g Express, LocalDB, SQL Server<br />

Compact, <strong>and</strong> SQL Azure. These two providers replace the SqlMembershipProvider <strong>and</strong> the<br />

SqlRoleProvider that shipped with earlier versions of <strong>ASP</strong>.<strong>NET</strong>.<br />

In the rema<strong>in</strong>der of this chapter, you see how to use the DefaultMembershipProvider <strong>and</strong> the<br />

DefaultRoleProvider. In the next chapter, you work with the DefaultProfileProvider. You can<br />

configure all three providers to use the same SQL Server database, mak<strong>in</strong>g it easy to centralize all<br />

your user data.<br />

Ideally, you don’t deal with these providers directly. Under normal circumstances, the various providers<br />

are configured for your website at a central location. You then use these providers by talk<strong>in</strong>g to<br />

the application services. Although you could access these services directly from code, you often use<br />

the <strong>ASP</strong>.<strong>NET</strong> built-<strong>in</strong> log<strong>in</strong> controls to do the hard work for you. These controls are discussed next.<br />

INTRODUCING THE LOGIN CONTROLS<br />

The log<strong>in</strong> controls that ship with <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> take away much of the complexity usually associated<br />

with writ<strong>in</strong>g a security layer <strong>in</strong> a website. The available log<strong>in</strong> controls effectively encapsulate all the<br />

code <strong>and</strong> logic you need to validate <strong>and</strong> manage users. These controls work by communicat<strong>in</strong>g with<br />

the configured provider through the application services, <strong>in</strong>stead of talk<strong>in</strong>g to a database directly.<br />

To see how this works, the follow<strong>in</strong>g exercise shows you how to create a simple Log<strong>in</strong> <strong>and</strong> Sign Up<br />

page that enables new users to create an account <strong>and</strong> then log <strong>in</strong>. The section that follows then looks<br />

at the seven log<strong>in</strong> controls that ship with <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong>.<br />

TRY IT OUT<br />

Creat<strong>in</strong>g Log<strong>in</strong> <strong>and</strong> Sign Up Pages<br />

In this Try It Out, you extend the Log<strong>in</strong> page that you created earlier. You also create a new page that<br />

enables a user to sign up for an account on the Planet Wrox website.<br />

1 . Open your website <strong>in</strong> VS <strong>and</strong> then open the Package Console Manager (choose Tools Í Library<br />

Package Manager Í Package Manager Console).<br />

2. Enter the follow<strong>in</strong>g comm<strong>and</strong> at the Package Manager Console prompt <strong>and</strong> press Enter:<br />

Install-Package Microsoft.AspNet.Providers<br />

By execut<strong>in</strong>g this package, VS downloads the providers, adds an assembly called System<br />

.Web.Providers.dll to the B<strong>in</strong> folder of your site <strong>and</strong> adds configuration <strong>in</strong>formation for the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!