Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Corey Roth

Related Topics: .NET, AJAX & REA

.NET: Article

Forms Authentication with a Twist of AJAX

A detour around redirects

On the client side, the confirmAuthenticated javascript function is responsible for reading the JSON formatted response generated from the code above. The JSON object created has a single property "isAuthenticated" that represents the current login status of the user. All client-side code can be found in Listing [ajaxlogin.js]. Now that we can check the login status without posting back, the only missing piece of the solution is the ability to log back in if necessary. Instead of relying on a separate form, these fields can be added to a hidden DIV of the existing page. As with any login page, there will have to be a username and password field for the user to enter data, along with a button to submit the data. Using the default.aspx page created earlier, an additional section has to be added as seen below. The entire contents of the default.aspx page can be found in listing [default.aspx]. (see Figure 2)

    <div class="loginDialog" style="display:none" id="loginDialog">
    <label for="username">UserName</label>
    <input type="text" id="username" /><br />
    <label for="password">Password</label>
    <input type="password" id="password" />
    <button onclick="javascript:ReLogin()">Login</button>
    </div>

Notice in this code that the button has assigned the ReLogin function to its click event. The purpose of the ReLogin function is just as it sounds. To keep this example simple, the username and password are simply submitted back to the login.aspx page. By carefully crafting the POST arguments to include the username, password, and the __EVENTTARGET, you can get the Login page to accept data that didn't actually originate from the page itself. Unfortunately, this requires turning off the EventValidation on the login page.

With all of the pieces now in place, let's take a look at the control flow for a typical request. We'll assume that the user has already successfully logged in and is viewing the default.aspx page. The user enters a value into the textbox and clicks the button. At this point, the onsubmit handler that was added to the form initiates an XmlHTTPRequest to check whether the user is logged in. If the user is logged in, the form submit can continue. However, if the user is no longer logged in, we must cancel the form submission. The form will be submitted again automatically, but only after the user enters the correct credentials.

The main difference here is that with the help of AJAX, we are now able to present the login screen on the same page the user is currently on instead of the previous default behavior that redirected the user away from the current page. This single change has improved the user experience dramatically and solved the data loss problem associated with redirects.

If you think hard enough these days, you can come up with a way to improve just about anything by using AJAX. It's quickly becoming the WD-40 of the ASP.NET world. With the emergence of client-side libraries such as Atlas, much of the hard-coded AJAX programming used here can be abstracted away. As these libraries continue evolve, even more time can be spent on logic with less time worrying about JSON and cross-browser compatibility.

What started out as a miserable user experience has been transformed into a streamlined pleasure, thanks to some well-placed AJAX. Taking this example to the next level, the code and functionality can all be packaged up into a single reusable control. Doing so will enable you to add this functionality quickly to any page in any project through a simple drag-and-drop action.

More Stories By Anthony Lombardo

Anthony Lombardo is the product manager of NetAdvantage for ASP.NET at Infragistics, Inc.  He is responsible for spearheading product management and strategies for Infragistics’ ASP.NET product line, working directly with the vice president of engineering to set the direction, plan, and manage product development. Prior to joining Infragistics, Anthony served as a Network Administrator for North Brunswick Board of Education and worked for Rutgers University in their technical support division. Since beginning his career with Infragistics in 2000, Anthony has been involved with every aspect of the development cycle, including playing a key role in the creation of the Presentation Layer Framework for ASP.NET, and determining the product feature set of NetAdvantage for ASP.NET.

 

Comments (4) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Rob 11/02/06 10:53:47 AM EST

It would be a good article if the source code was complete. Is there anyway to get a copy of the complete source code? The ajaxlogin.js is missing.

Paul 10/19/06 04:28:18 PM EDT

Good article, but your source code was incomplete; the zip didn't include the .js file which is where most of the functionality appears to be...

Bob Baker 09/22/06 02:12:20 PM EDT

Forms Authentication with a Twist of AJAX article has some problems. Both the Figure 1 online link and the magazine article reference point to the VS2005 project setup dialog Figure. Worse, Listing 1 download does not include ajaxlogin.js. Where is this file? Also, what is a valid username and password for this site?

j j 09/22/06 11:34:44 AM EDT

Forms Authentication for ASP.NET is extremely powerful in that it lets you quickly add a layer of security to your Web site. While the simplicity of setup and implementation makes this form of authentication extremely attractive, usability can sometimes be downright ugly. The core functionality of Forms Authentication relies on redirects - first redirecting anonymous requests to the login page, and then redirecting back to the originally requested resource. Constant redirects not only annoy users, they can disrupt your page logic. Mixing in some AJAX with your Forms Authentication will quickly eliminate the need for most redirects, and the associated negative effects.