| By Anthony Lombardo | Article Rating: |
|
| September 22, 2006 11:15 AM EDT | Reads: |
18,171 |
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.
Published September 22, 2006 Reads 18,171
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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. |
||||
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications































