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

Last post 12-29-2007 4:10 AM by aadreja. 1 replies.

Sort Posts:

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

    12-29-2007, 3:41 AM
    • Participant
      1,635 point Participant
    • JohanNL
    • Member since 03-28-2003, 8:58 AM
    • Posts 1,195

    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?

    Johan Theunissen
    MCPD, MCSE, MCTS BizTalk 2006

    ==============================

    Please mark the most helpful reply/replies as "Answer".
  • Re: System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed

    12-29-2007, 4:10 AM
    Answer
    • Contributor
      2,090 point Contributor
    • aadreja
    • Member since 02-03-2007, 6:36 AM
    • Ahmedabad, India
    • Posts 354
    Thanks,
    Ritesh

    ---------------------------------------------
    Please do not forget to mark as Answer if my reply is helpful.
Page 1 of 1 (2 items)