37

We know that form authentication cookie is encrypted. so how to read the form authentication cookie content from my code behind.

if (Request.Cookies[".ASPXAUTH"] != null)
{
    HttpCookie myCookie = new HttpCookie(".ASPXAUTH");
}
1

1 Answer 1

87

You can access the ticket with the Decrypt method provided by FormsAuthentication

HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);

string cookiePath = ticket.CookiePath;
DateTime expiration = ticket.Expiration;
bool expired = ticket.Expired;
bool isPersistent = ticket.IsPersistent;
DateTime issueDate = ticket.IssueDate;
string name = ticket.Name;
string userData = ticket.UserData;
int version = ticket.Version;
Sign up to request clarification or add additional context in comments.

5 Comments

How to extract data from ticket....suppose i create auth cookie like below one way. FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, username, DateTime.Now, DateTime.Now.AddMinutes(30), isPersistent, userData, FormsAuthentication.FormsCookiePath); so how get the above data from ticket after decrypt ? plzz guide me.......thanks
After decrypt, you can access properties directly on the ticket. This shows the list of properties: msdn.microsoft.com/en-us/library/… . I updated the answer with an example of accessing the data in the ticket.
just a typo: ticket.IsPersistant should be ticket.IsPersistent
Thanks for this. Also, I believe on last line, version should be int, not string. But I am using ASP.NET 4.5....
This is brilliant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.