0
votes

I am trying to create and read a forms authentication cookie in a c# web app that I am developing.

I create the ticket

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, "myData", DateTime.Now, DateTime.Now.AddMinutes(60), true, "Hello");

// Now encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);

// Add the cookie to the outgoing cookies collection Response.Cookies.Add(authCookie);

Then when I retrieve the ticket using:

HttpCookie authCookie = Context.Request.Cookies[cookieName];

FormsAuthenticationTicket authTicket = null;

authTicket = FormsAuthentication.Decrypt(authCookie.Value)

I can see authTicket now has all of the data, cookie creation date, expiration date, name="mydata"... etc.

But there is nothing in the dataValue... I am expecting "Hello" to be there.

When I debug, I can see it is in the ticket right before encryption... it is getting lost in the decryption I suppose?

Any Help?

2

2 Answers

0
votes

Try using SetAuthCookie(authCookie) instead of REsponse.Cookies.Add(authCookie).

http://msdn.microsoft.com/en-us/library/aa480476.aspx

0
votes

In order to set the cookie you use the FormsAuthentication.FormsCookieName property while when you read it you use the cookieName variable. Are you sure that both point to the same cookie? Also verify with FireBug that the value in the cookie is the same as the one you see when you debug.