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

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s