PBDJ APRIL 2009 | POWERBUILDER.SYS-CON.COM
PAGE 6
POWERBUILDER.SYS-CON.COM | PBDJ APRIL 2009
PAGE 7
modifi cations. Even after putting in that effort,
you often fi nd yourself having to modify the code
and user interface components that consume the
membership data, such as screens for logging in,
editing user profi les, retrieving passwords, etc.
It was with this hardship in mind that the SQL
Membership Provider was born.
Using Visual Studio and a simple command-
line command, a Web site can be created that
provides functionality and user interface com-
ponents for authentication, profi le editing, and
password management capabilities using SQL
Server 2005 or SQL Server Express as a data store.
Passwords are secured by being stored hashed
or encrypted, by enforcing a “password lockout”
policy, and by requiring security questions and
answers to reset. The best part is that all of this
functionality can be provided without the devel-
oper writing a single line of code.
There are not many cases where “one size fi ts
all,” and membership functionality certainly falls
into that category. Fortunately, customizing the
Membership Provider is almost as simple as using
its default confi guration. Basic customization can
be handled in the confi guration fi le for the Web
application, still not requiring any user-written
code. For instance, you can confi gure whether
a security question must be answered before a
password is reset by setting the requiresQues-
tionAndAnswer attribute of the membership
provider’s “add” element. The PasswordRecovery
control then works with the membership provider
and adjusts its user interface by adding steps to
the password recovery “wizard” to prompt for the
security question confi gured by the user and to
validate the answer provided.
If the application needs to draw on information
from a proprietary source, such as a company’s
internal user management application, a custom
membership provider can be created. When
implementing a membership provider, the devel-
oper is responsible for, and has complete control
over, all aspects of the process. From connectiv-
ity to the data source, to password protection, to
creating new users; each step requires explicit
implementation. Fortunately, once completed,
the provider can be plugged into the Web site via
the Web confi guration and will work seamlessly
with the existing membership controls. While this
method offers a great deal of control and fl exibil-
ity, it requires a fair amount of effort for someone
who needs only minor functionality changes that
are not confi gurable using the existing providers.
Thanks to the provider model and object-ori-
ented programming, there’s a much easier middle
ground.
To demonstrate the ease with which a Mem-
bership Provider can be customized, it’s best to
consider a real-world example. The standard SQL
Membership Provider has a great deal of function-
ality to protect and manage Web site passwords.
With nothing more than confi guration, passwords
can be protected and stored using a one-way
hash so that even database administrators cannot
retrieve them. To grant access to a user who has
forgotten his password, a new password must be
created. This is typically done via the Password
Reset Web Control, which implicitly uses the Reset-
Password method of the SQL Membership Provider
to auto-generate a new password and send it to the
user in an e-mail message.
Your password has been reset. Please return to the site and
log in using the following information.
User Name: JoePiccirilli
Password: :mits(*^w?C[@m
As you can see, the default reset password func-
tionality creates a strong password based on ran-
dom characters that include letters, numbers, and
special characters. While this implementation
offers great protection, it also has many usability
issues. For a user to type this password, he has to
distinguish between characters that appear simi-
lar (1 vs. I, 0 vs. O, etc.) Some users might even be
challenged to simply fi nd the characters in ques-
tion. Even the option of copy-and-paste falls short
if you aren’t careful. The default selection logic
of many mail clients will try to select all of the
characters after and excluding the leading colon
“:” in the password above. After too many invalid
attempts, the user could be locked out of the
site, causing more frustration. The Membership
Provider gives you some control over passwords,
such as minimum password length, minimum
required non-alphanumeric characters; even
a password-strength regular expression. If you
want to allow strong passwords while resetting to
more basic passwords, you need to move beyond
the native capabilities of the SQL Membership
Provider.
To customize the SQL Membership Provider,
you start by creating a new class that inherits
from the existing SQLMembershipProvider class.
public class MembershipProvider : SQLMembershipProvider
FEATURE
W
ith the release of ASP.NET 2.0, Microsoft
introduced Web developers to the “pro-
vider” model that addresses common
application infrastructure needs using a system of
pluggable modules adhering to common inter-
faces.
ASP.NET shipped with modules to cover Mem-
bership (authentication), Roles (authorization),
Sitemap (navigation), and others. These mod-
ules work with each other, and with many of the
controls included in Visual Studio, to drastically
reduce the amount of code needed to perform
the functions that nearly every Web application
requires.
Each provider shipped with multiple imple-
mentations to cover the most common develop-
ment and deployment scenarios. More impor-
tantly, the providers were built in a way that lets
developers customize key elements of functional-
ity without having to write an entire provider from
scratch and without having to worry about any
other code that consumes it.
I will be talking specifi cally about the SQL
Membership provider that handles authentica-
tion and user management functionality using a
SQL Server database as the membership reposi-
tory. I’ll provide examples of how to customize the
behavior of the provider using very little code.
Anyone developing membership-based Web
sites has likely found himself writing code to
manage users. Those who are industrious enough
(or simply tired of the pain) may take it one step
further and write more generic code that can
be plugged into different sites with only minor
Customizing the .NET SQL
Membership Provider
How to customize the behavior of the provider
using very little code
JOE PICCIRILLI