| By Mujtaba Syed | Article Rating: |
|
| April 6, 2004 12:00 AM EDT | Reads: |
32,025 |
Building security into intranet Web applications was always easy: just turn on Windows authentication in IIS. But considering the size of the user base for Internet Web applications, custom form-based authentication is the only scalable solution.
If you have built Internet Web applications with ASP 3.0, you know the amount of effort that went into adding form-based authentication. You pretty much had to do everything - build the login form; check for the username and password validity, and if they were valid, write the authentication cookie to the cookies collection of the Response object. Then you had to check for the authentication cookie at the beginning of every page and, if it wasn't present, you had to redirect the user to the login page. And if you wanted to use role-based authorization in the application, you had to write even more code!
The advent of ASP.NET 1.0 brought much-needed relief to ASP developers. By modifying an XML configuration file (Web.config), most of the drudgery of adding form-based authentication was eliminated. But you still had to create the login form, create and maintain a user credential store, and check for user-ID and password validity. And you had to write some additional custom code if you wanted role-based authentication.
ASP.NET 2.0, codenamed "ASP.NET Whidbey," adds a bunch of new features that make building forms-based authentication into applications even faster and simpler.
Membership
First, ASP.NET Whidbey adds the Membership API, which is a user-credentials management interface. The user credentials are stored in a user credentials store. The membership API speaks to the credential store via a provider. ASP.NET Whidbey comes prepackaged with two providers - one for Access and the other for SQL Server. You can write your own custom provider to speak to a user credential store of your choice (for instance, an XML database). The Access provider is the default provider, as shown in Machine.config (see Listing 1).
The heart of the Membership API is the Membership (System.Web.Security.Membership) class. The Membership class is a sealed class that has mostly static properties and methods. Following are some of the commonly used methods of the Membership class:
- CreateUser: Used to create a new user.
- GetUser: Gets the details of a particular user. Returns an instance of the MembershipUser (Sys-tem.Web.Security.MembershipUser) type.
- UpdateUser: Updates user details in the user credentials store. This method accepts an instance of the MembershipUser type.
- ValidateUser: Takes a user's credentials (username and password) and returns true if the credentials are valid, and false if they are not.
The MembershipUser class is another important class. An instance of this class represents a valid user in the application. This class has properties that provide access to user details like the username, e-mail address, and password question. It also has methods that allow you to, among other things, reset the user password, change the user password, and change the password question and answer.
Role Management
Apart from user authentication, most application logic requires some kind of role-based authorization. The Role manager also uses some kind of provider to store the information related to roles and their mappings to users.
The Access provider is the default provider, as seen in Machine.config (see Listing 2). You can change the provider to SQL Server or a custom store by making appropriate changes to Web.config.
The Roles (System.Web.Security.Roles) class can be used to access this information. Some of the important methods of the Roles class are as follows:
- CreateRole: Used to create a new role.
- GetAllRoles: Used to get all existing roles. It returns an array of strings.
- AddUserToRole: Adds a user to a role.
- RemoveUserFromRole: Removes a user from a role.
- IsUserInRole: Checks if a user belongs to a role.
- GetRolesForUser: Gets all roles for a particular user. It returns an array of strings.
ASP.NET Whidbey introduces five new server controls specifically designed to reduce the amount of code that developers have to write to add forms-based authentication to their Web applications. Let's drill down into each of these controls, one at a time.
The Login Control
The Login control provides a ready-made login UI that can be embedded in a form, as shown in the following code snippet:
<%@ page language="C#" %>
<script runat="server">
</script>
<html>
<head runat="server">
<title>Login Page</title>
</head>
<body>
<form runat="server">
<asp:login id="login" runat="Server" />
</form>
</body>
</html>
The output produced by this code is shown in Figure 1.
This is the most basic look provided by the Login control. To achieve this same effect in ASP.NET 1.1 would require about 15 lines of code. The Login control also provides auto-formatting ability through smart tags, as shown in Figure 2.
The Login control fires the authenticate event when the "Log In" button is clicked. This event can be connected to an event handler, as shown in the following code snippet:
<asp:login id="login" runat="server" onauthenticate="AuthenticateUser" />
The event handler validates the username and password provided by the user using the Membership class's ValidateUser method, as shown below. If the credentials provided are valid, the user is redirected away from the login page, as shown in the following code snippet:
void login_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
{
if (Membership.ValidateUser (login.UserName, login.Password))
FormsAuthentication.RedirectFromLoginPage (login.UserName, false);
else
login.FailureText = "Login failed. Please try again.";
}
The LoginName Control
The LoginName control displays the name of the logged-in user (User.Identity.Name). It is used as shown below:
<asp:loginname id="loginName" runat="server" />
You could also display a customized greeting by setting the formatstring property of the LoginName control:
<asp:loginname id="loginName" runat="server" formatstring="Welcome, {0}."/>
The LoginStatus Control
If the user is logged in, the LoginStatus control displays a "Logout" link; if the user is logged out, the LoginStatus displays a "Login" link. The link can also be made to show login and logout images. It's used as shown in the following code snippet:
<asp:loginstatus id="loginStatus" runat="server" />
The LoginView Control
The LoginView control is a templated control that has two templates - a template for an anonymous user and a template for a logged-in user. It can also be used to display content based on a user's assigned roles.
Usually, the LoginView control will be used with the LoginName control in its "loggedintemplate", as shown below:
<asp:loginview id="loginView" runat="server">
<anonymoustemplate >
Welcome, Guest!
</anonymoustemplate>
<loggedintemplate>
<asp:LoginName id="loginName" runat="server" formatstring="Welcome, {0}." />
</loggedintemplate>
<asp:rolegroup roles="Price">
Content for a prince!
</asp:rolegroup>
<asp:rolegroup roles="Pauper">
Content for a pauper!
</asp:rolegroup>
</asp:loginview>
The PasswordRecovery Control
People tend to forget passwords. Therefore, most Web sites provide a password-recovery feature. In the case of a forgotten password, this functionality mails the user:
- A random password, or
- The existing password, only after the user has provided the correct answer to the password question (which was set up during signup)
<asp:passwordrecovery id="passwordrecovery" runat="server">
<maildefinition from="support@fooinc.com" />
</asp:passwordrecovery>
Figure 3 shows how this control appears in the browser.
To allow the user to retrieve the current password, the following changes must be made in the <membership> section of Web.config: the enablePasswordRetrieval attribute has to be set to true, and the passwordFormat attribute has to be set to "Clear" or "Encrypted" (as opposed to "Hashed", which is a one-way encryption that makes it impossible to retrieve the password). To allow the recovery of the current password by providing the answer to the password question, the requiresQuestionAndAnswer property must also be set to true.
By setting only the enableReset-Password attribute of the <membership> section to true, the user has the ability to request a new random password.
Conclusion
This was a brief walkthrough of the new security features introduced in ASP.NET Whidbey. After reading through this article, it should be clear to all that no one knows the pulse of Web developers better than the ASP.NET team.
Published April 6, 2004 Reads 32,025
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Mujtaba Syed
Mujtaba Syed works as a software architect with Marlabs Inc. He is an MCSD
(early achiever) and loves to speak about and write on Microsoft .NET. Mujtaba has been programming the Microsoft .NET Framework since its beta 1 release. His current interests are focused on Longhorn.
![]() |
Venu 08/24/07 06:31:37 PM EDT | |||
Easy to understand article. |
||||
- iPad3 vs Windows 8 - and the Winner Is...Cloud
- Eleven Reasons Why Windows Phone Will Overtake Android
- Windows Azure Overview Part 4: Security
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- Eleven Tips for Successful Cloud Computing Adoption
- GM to Pull Facebook Advertising: WSJ
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Apply Agile When Deploying Apps
- The Web – Changing the Way We Work
- EE Times and EDN Announce the 2012 UBM Electronics ACE Award Winners
- Closer Look at One NoSQL Database – MongoDB
- Why Is Scrum So Widely Adopted and So Very Dangerously Deceptive
- iPad3 vs Windows 8 - and the Winner Is...Cloud
- Cisco Unveils Visual Collaboration Solutions in the Post-PC Era, Extending the Reach of TelePresence With New Mobile-to-Immersive Offerings
- Eleven Reasons Why Windows Phone Will Overtake Android
- Windows Azure Overview Part 4: Security
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- Eleven Tips for Successful Cloud Computing Adoption
- GM to Pull Facebook Advertising: WSJ
- System Center Virtual Machine Manager 2012 as Private Cloud Enabler
- Apply Agile When Deploying Apps
- The Web – Changing the Way We Work
- Book Review: Decision Management Systems
- User Group Malaise?
- Google Maps and ASP.NET
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- Crystal Reports XI & How It Has Changed
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- Programmatically Posting Data to ASP .NET Web Applications
- Implementing Tab Navigation with ASP.NET 2.0
- AJAX World RIA Conference & Expo Kicks Off in New York City
- i-Technology Viewpoint: "SOA Sucks"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"























