Example #1
0
        public static int SetAuthCookie <T>(this HttpResponseBase responseBase, string name, bool rememberMe, T userData)
        {
            var cookie = FormsAuthentication.GetAuthCookie(name, rememberMe);
            var ticket = FormsAuthentication.Decrypt(cookie.Value);

            if (ticket == null)
            {
                return(0);
            }

            var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration,
                                                          ticket.IsPersistent, string.Empty, ticket.CookiePath);
            var encryptedTicket = FormsAuthentication.Encrypt(newTicket);

            if (encryptedTicket == null)
            {
                return(0);
            }

            if (!string.IsNullOrEmpty(FormsAuthentication.CookieDomain))
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }

            cookie.Value = encryptedTicket;
            responseBase.Cookies.Add(cookie);
            responseBase.SetCookieData(UserDataCookieName, userData);

            return(encryptedTicket.Length);
        }