Scott Hanselman

New FormsAuthentication.SignOut behavior in ASP.NET 2.0

July 12, '05 Comments [6] Posted in ASP.NET | DasBlog
Sponsored By

Calling FormsAuthentication.SignOut() removes the FormsAuthentication cookies in ASP.NET 1.1. Sometimes folks call it from their Login page, like this:

if (!Page.IsPostBack)

This says, "If this is a fresh load of this page, clear out the authentication cookies."

If this seems like a reasonable thing, that's because it is. However, if you run an ASP.NET 1.1 site under ASP.NET 2.0 without recompiling, as a number of dasBlog users do, you may get some odd behavior.

You'll visit the Login.aspx page and redirect to the Login.aspx page forever in a will make your URL look like this:


Why? Because ASP.NET 2.0 add this code inside SignOut():

if (FormsAuthentication.CookieMode != HttpCookieMode.UseCookies)
    Response.Redirect(FormsAuthentication.GetLoginPage(null), false);

That's pretty interesting. If you call SignOut() it redirects you to the Login page, but only if your CookieMode isn't set to UseCookies. This is because ASP.NET 2.0 added support for cookieless FormsAuthentication. They store the auth information in the URL, and they redirect you because they want to clear the authentication info. Makes sense.

What doesn't make sense is why HttpCookieMode doesn't default to UseCookies. It defaults to Cookieless. Which is lovely under ASP.NET 2.0, but not under 1.1. It stays that way and confuses the system.

So, if you see this kind of infinite redirect with FormsAuthentication while running ASP.NET 1.1 applications under ASP.NET 2.0, you can add cookieless="UseCookies" to your <forms> element in web.config:

<forms cookieless="UseCookies" name=".DASBLOGAUTH" protection="All" timeout="60" path="/"/>

And then I went to sleep.

Now playing: Counting Crows - Angels of the Silences

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Tuesday, 12 July 2005 06:08:59 UTC
The loooooong URL above makes the page be about 10,000 pixels wide.
Tuesday, 12 July 2005 14:22:27 UTC
Is it just me or does anyone else think this line is weird?

Jiho Han
Tuesday, 12 July 2005 14:28:39 UTC
Great FormsAuthentication double-dip today, Scott. I gotta go revisit some code right away!
Sergio Pereira
Friday, 15 July 2005 19:52:32 UTC
Why do you say it's lovely that ASP.NET 2.0 defaults to cookieless?
Bob Uva
Friday, 15 July 2005 19:55:29 UTC
It's sarcasm. It's NOT lovely when running a 1.1 app because of this blog post.
Scott Hanselman
Friday, 09 September 2005 15:57:58 UTC
cookieless="UseCookies" ... yes thats a little strange, not sure why it wouldnt be cookieless="false"; but hey, it works, so what do i care? *shrugs* Thanks Scott!
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.