Link to home
Start Free TrialLog in
Avatar of mattbelfast23
mattbelfast23

asked on

Cookie Encryption and Decryption giving errors

Hi all,

This following problem has really started to frustrate me.  Being new to the platform ive largely been using quickstart and tutorial code to create a login system for a website im developing.  The problem is that cookie encryption seems to be not working at all.  I have been unable to locate any previous examples of my error.  Please Help

// Login code
// This just places some user details into a pipe separated list e.g.
// 1|DFGEG34534sgdfgdfg34|Admin|Member|Visitor
string strUserData = objMember.MemberCookie();

FormsAuthenticationTicket objTicket = new FormsAuthenticationTicket(1,
      objMember.strFirstname,
      System.DateTime.Now,
      System.DateTime.Now.AddHours(12),
      true,
      strUserData,
      FormsAuthentication.FormsCookiePath);

// THIS FUNCTION IS RETURNING NULL EVERY TIME.  Im not sure why
string strHash = FormsAuthentication.Encrypt(objTicket);                  
if ( null == strHash)
{
      throw new ApplicationException("An error occurred while hashing the user data.");
}

HttpCookie objCookie = new HttpCookie(FormsAuthentication.FormsCookieName, strHash);

objCookie.Expires = System.DateTime.Now.AddMonths(2);
System.Web.HttpContext context = System.Web.HttpContext.Current;
context.Response.Cookies.Add(objCookie);

FormsAuthentication.RedirectFromLoginPage( objMember.strFirstname, true );


// Authenticate_Request Code
string strCookieName = FormsAuthentication.FormsCookieName;
HttpCookie objCookie = Context.Request.Cookies[strCookieName];

if(null == objCookie)
{
      // There is no authentication cookie.
      return;
}

FormsAuthenticationTicket objTicket = null;
try
{      
      // BREAKING IN HERE AGAIN
      objTicket = FormsAuthentication.Decrypt(objCookie.Value);
}
catch(Exception ex)
{
throw new ApplicationException("An error occurred while hashing the user data." + (ex).ToString());
}

if (null == objTicket)
{
      // Cookie failed to decrypt.
      return;
}

// gets the pipe separated cookie data
Components.MemberDetails objMember = new Components.MemberDetails();
objMember.RetrieveCookieData(objTicket.UserData);

// Parse the role names
string[] arrRoles = (objMember.strMemberRoles).Split(new char[]{'|'});

// Create an Identity object
FormsIdentity objID = new FormsIdentity( objTicket );

// This principal will flow throughout the request.
GenericPrincipal objPrincipal = new GenericPrincipal(objID, arrRoles);
// Attach the new principal object to the current HttpContext object
Context.User = objPrincipal;

Any help would be much appreciated
Cheers
Matt
Avatar of daffodils
daffodils

Some suggestions.. off the top right now..

Try using "string.Empty"
if ( strHash == string.Empty)
{
     throw new ApplicationException("An error occurred while hashing the user data.");
}

And is the cookie being generated at all... encryption or not.. can you see it in the folder specified as path.
Is javascript enabled on your client browser?
Avatar of mattbelfast23

ASKER

objTicket = FormsAuthentication.Decrypt(objCookie.Value);

is giving the following error

System.ArgumentException: Invalid value for 'encryptedTicket' parameter.
>>objTicket = FormsAuthentication.Decrypt(objCookie.Value);

That's probably because it is expecting the encrypted authentication ticket.. use just the ticket (cookie)..

objTicket = FormsAuthentication.Decrypt(objCookie);
Its writing the data without encryption too
so Encrypt is not working !
okay.. give me a minute here, maybe MSDN would help..
SOLUTION
Avatar of daffodils
daffodils

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works for normal cookie data 11|DOG|1,2,3 works fine.  When i introduce the encryption it all goes pear shaped :(
Help me here..
Does encryption work for normal cookie data like strUserData = "11|DOG|1,2,3" ??



Sorry, no the encryption doesnt work.  And the Decryption is giving errors

// Not encrypting
string strHash = FormsAuthentication.Encrypt(objTicket);                  
if ( string.Empty == strHash)
{
     return;
}

the login code with encyption is giving this:  Notice the blank line betweenthe cookie name where the data should be

.SA

localhost/
1024
1572977536
29662059
754716720
29662055
*

Application_AuthenticateRequest

// Gives this error System.ArgumentException: Invalid value for 'encryptedTicket' parameter.
objTicket = FormsAuthentication.Decrypt(System.Web.HttpUtility.UrlDecode(objCookie.Value));

Thanks for your help so far by the way :)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Finally got this to work.  It appears it was the "using" statements although how they were causing it to break i have no idea.  I had the correct statements in from the start, it seems that they just didnt work for some reason. Maybe i had them in the wrong order or something.

Anyways thanks for your help and patience
Hmm... "using" statements !  Good that it worked out for you :)
Best of Luck...