Forms Authentication and Cookies

Forms Authentication and Cookies

by blamoreaux 15. August 2007 09:07

I encountered a strange problem this morning when I tried to turn on Authentication to the ASP.NET 2.0 Website. I opened my handy dandy Wrox Professional ASP.NET 2.0 book to chapter 18, Security, and started the simple process of implementing forms authentication. Simple, hah! What happen was my cookies were not being saved. Walking step by step, I first made sure I denied access to any anonymous users.

<authorization>

  <deny users="?" />

</authorization>

Piece of Cake! I then added my code to redirect to a logon page. (Note: It shouldn't make a difference using "Mock.aspx" instead of "Logon.aspx")

<authentication mode="Forms">

  <forms loginUrl="Mock.aspx" path="\" />

</authentication>

Inside of Mock.aspx is the code to always authenticate everyone as the same user.

   10 protected void Submit_Click(object sender, EventArgs e)

   11    {

   12         // Always use the same user

   13         string userName = "Charle";

   14         FormsAuthentication.RedirectFromLoginPage(userName, true);

   15    }

What happened next was unexpected. I tried to hit my default page and was redirected to the logon page. Good. I clicked the Submit button and poof! I'm back at the logon form. That's weird I was expecting to go to my default page.

Using fiddler, I found that my cookie wasn't being persisted between called. The logon page was setting one but it was lost on the other side.

After trying several things, I changed my web.config file to the following:

<authentication mode="Forms">

  <forms name=".ASPXAUTH"

         loginUrl="Mock.aspx"

         protection="All"

         timeout="30"

         path="/"

         requireSSL="false"

         slidingExpiration="true"

         defaultUrl="default.aspx"

         cookieless="UseCookies" />

</authentication>

Making this change made my cookies stick. I'll have to dig in deeper to see why the first way didn't work.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

November 20. 2008 00:49

Gravatar

Powered by BlogEngine.NET 1.1.0.7
Theme by Mads Kristensen

Subscribe

About the author

Brig Lamoraeux Brig Lamoreaux
I'm a .Net developer.

E-mail me Send mail

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Pages

Recent posts

Recent comments

Tags

Categories


Archive

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2006-2008

Sign in