|
Comments
|
Today's Top SOA Links
ASP.NET Quick and Easy Custom Form-Based Authentication
The new membership, role management, and security features of ASP.NET 'Whidbey'
By: Mujtaba Syed
Apr. 6, 2004 12:00 AM
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 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:
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 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:
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 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:
The LoginName Control <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 <asp:loginstatus id="loginStatus" runat="server" /> The LoginView Control Usually, the LoginView control will be used with the LoginName control in its "loggedintemplate", as shown below:
The PasswordRecovery Control
<asp:passwordrecovery id="passwordrecovery" runat="server"> 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 Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||