title: System.ArgumentException: Invalid value for 'encryptedTicket' parameter happens on any login after the first successful login
body: <p>I am currently trying to replace our company wide user authentication that we use for all our internal web apps and what not as our current one was made in 2006 and fails on the regular. I was told to make it as simple as possible to implement on all existing projects. It is a .NET class library. It's .dll will be added as a reference to existing projects.</p>
  
  <p>I am having an issue where I can log in exactly one time after all cookies have been cleared. Once I logout and log back in I get <code>System.ArgumentException: Invalid value for 'encryptedTicket' parameter.</code> I found some posts suggesting the cookie may be null, or I'm not trying to decrypt the name and not the value, but that wasn't the case. This happens on chrome and edge. </p>
  
  <p>The user is authenticated every time though, assuming the correct username and password is used as I get redirected to the success page.</p>
  
  <p>After authentication I add a cookie and then redirect.</p>
  
  <pre><code>    private void AddCookie(int compID, bool persist, HttpContext httpContext)
      {
          httpContext.Request.Cookies.Add(SetUpSession(compID, persist));
          FormsAuthentication.RedirectFromLoginPage(compID.ToString(), persist);
      }
  </code></pre>
  
  <p>My method for creating the cookie</p>
  
  <pre><code>private HttpCookie SetUpSession(int companyID, bool persist)
          {
              FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                                                      1,                                     // ticket version
                                                      companyID.ToString(),                  // authenticated username
                                                      DateTime.Now,                          // issueDate
                                                      DateTime.Now.AddMinutes(30),           // expiryDate
                                                      persist,                               // true to persist across browser sessions                                                    
                                                      FormsAuthentication.FormsCookiePath);  // the path for the cookie
  
              String encTick = FormsAuthentication.Encrypt(ticket);
              HttpCookie cookie = new HttpCookie("Cookie", encTick);
              cookie.HttpOnly = true;
              return cookie;
          }
  </code></pre>
  
  <p>After I redirect to the success page there is a snipped of code that checks to see if the user is logged in. This is where the error happens</p>
  
  <pre><code>public dynamic isLoggedIn(HttpContext httpContext)
          {
              AuthenticationUtilities authUtil = new AuthenticationUtilities();
              if (httpContext.Response.Cookies["Cookie"] != null)
              {
                  companyID = authUtil.Authenticate(httpContext.Request.Cookies["Cookie"]);//the error occurs here
                  authUtil = new AuthenticationUtilities(companyID);
                  return authUtil;
              }
              else
              {
                  httpContext.Response.Redirect("~/login.aspx");
                  return null;
              }
          }
  </code></pre>
  
  <p>The method that decrypts the cookie</p>
  
  <pre><code>public int Authenticate(HttpCookie cookie)
          {
               FormsAuthenticationTicket authTick = FormsAuthentication.Decrypt(cookie.Value);
  
               return int.Parse(authTick.Name);
          }
  </code></pre>
  
  <p>this method is called on any page that requires the user to be logged in, like this.</p>
  
  <pre><code>LMFJAuth.AuthenticationUtilities auth = _LMFJAuth.isLoggedIn(HttpContext.Current);//if the cookie is null it redirects to login. 
  </code></pre>
  
  <p>This is the logout method</p>
  
  <pre><code> public void LogOut(HttpContext httpContext)
          {
              FormsAuthentication.SignOut();
              HttpCookie cookie = new HttpCookie("Cookie");
              cookie.Expires = DateTime.Now.AddMinutes(-1);
              httpContext.Session.Clear();
              httpContext.Response.Cookies.Add(cookie);
              httpContext.Response.Redirect(FormsAuthentication.LoginUrl);
          }
  </code></pre>
  
  <p>Can somone help explain what may be going on in which the value for the encrypted ticked is coming up as invalid after the first successful login/logout?</p>