Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Ajax and forms authentication

Forms authentication is nice way to protect your asp.net web pages from unauthorized views. The good thing is that it shields all request for pages in your site and will redirect the request to a login page. You can set the time out of a session, after a preset period of inactivity the user has to be re-authenticated.

Al done in the web.config

<authentication mode="Forms">

   <forms loginUrl="~/login.aspx" timeout="2">

</forms>

</authentication>

<authorization>

   <deny users="?"/>

</authorization>

 

The bad thing is that forms authentication does not work that well with an AJAX site. When a partial postback hits the server and the session has timed out the server will redirect the request to the login page. This is a response the AJAX request cannot handle well. The result will be an endless loop of requests and the page just hangs. Damit Dobric has a very informative post on this. The good thing about Firefox is that it does detect the redirect loop and will stop. IE just keeps on trying.

Damir presents a solution for the problem which does requires quite some fiddling. Here I would like to present a simpler solution.

The page load of the masterpage checks if the request is the first one in the current session by inspecting the Session.IsNewSession property. In a page shielded with forms authentication this will never be the case; posting back the login form was the first request. But when the request was issued by a partial postback in a timed out session the IsNewSession property will read true and the situation can be handled.

protected void Page_Load(object sender, EventArgs e)
{
    // Ajax postback, session timed out. Redirect 
    if (Session.IsNewSession)
    {
        FormsAuthentication.SignOut();
        Response.Redirect("~/Default.aspx", true);
    }
}

It explicitly signs out of FormAuthentication and redirects the user to the main page. No more loops, no more hangups.


Posted 12-18-2008 9:46 AM by pvanooijen

[Advertisement]

Comments

Alastair Smith wrote re: Ajax and forms authentication
on 12-18-2008 5:13 AM

Nice trick!  :-)  This looks like a more elegant solution than Damir's.  

Peter's Gekko wrote Silverlight, FireFox and 100%
on 12-18-2008 9:08 AM

In my last post I mentioned something nice about FireFox. In this post I&#39;ll talk something not that

Community Blogs wrote Silverlight, FireFox and 100%
on 12-18-2008 9:37 AM

In my last post I mentioned something nice about FireFox. In this post I&#39;ll talk something not that

DotNetKicks.com wrote Ajax and forms authentication
on 12-18-2008 10:27 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Dew Drop - December 18, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - December 18, 2008 | Alvin Ashcraft's Morning Dew
on 12-18-2008 10:46 AM

Pingback from  Dew Drop - December 18, 2008 | Alvin Ashcraft's Morning Dew

rascunho » Blog Archive » links for 2008-12-18 wrote rascunho &raquo; Blog Archive &raquo; links for 2008-12-18
on 12-18-2008 3:05 PM

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-12-18

Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas wrote Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas
on 12-23-2008 9:01 AM

Pingback from  Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas

Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas wrote Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas
on 12-24-2008 9:05 AM

Pingback from  Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas

Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas wrote Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas
on 12-25-2008 6:20 AM

Pingback from  Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas

Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas wrote Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas
on 12-30-2008 6:02 AM

Pingback from  Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas

Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas wrote Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas
on 01-01-2009 7:26 AM

Pingback from  Silverlight, FireFox and 100% - taccato! trend tracker, cool hunting, new business ideas

Peter's Gekko wrote Keeping a long running Silverlight application alive under forms authentication
on 04-16-2009 4:30 PM

This is the scenario : we have a Silverlight application (SL app) which run is running unattended on

Add a Comment

(required)  
(optional)
(required)  
Remember Me?