System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed

December 28, 2007 at 9:16 am | In ASP.NET Tips | Leave a Comment

I have the subject error on the following line of code:

FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Request.Cookies["UserData"].Value);

===== 

The next code sets the cookie:

bool isPersistent;

// if the user want to be remembered next time

if (this.RememberMe.Checked == true)

{

FormsAuthentication.SetAuthCookie(this.txtLogonName.Text, true);

isPersistent = true;

}

else

{

FormsAuthentication.SetAuthCookie(this.txtLogonName.Text, false);

isPersistent = false;

}

 

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,

txtLogonName.Text,

DateTime.Now,

DateTime.Now.AddDays(30),

isPersistent,

string.Empty,

FormsAuthentication.FormsCookiePath);

// Encrypt the ticket

string encryptedTicket = FormsAuthentication.Encrypt(ticket);

// Create the cookie

HttpCookie cookie = new HttpCookie("UserData", encryptedTicket);

cookie.Expires = DateTime.Now.AddDays(30);

cookie.Path = "/";

Response.Cookies.Add(cookie);

Response.Redirect("secure/Default.aspx", false);

}

What causes this error?

Read More

No Comments Yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.