public static void SetAuthCookie(string userName, MyFormsAuthentication userData, bool isRemember)
        {
            if (userData == null)
            {
                throw new ArgumentNullException("userData");
            }

            var      data    = JsonConvert.SerializeObject(userData);
            DateTime expires = isRemember ? DateTime.Now.AddDays(30) : DateTime.Now.AddDays(1);

            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, expires, isRemember, data);
            string     cookieValue           = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue)
            {
                HttpOnly = false,
                Domain   = FormsAuthentication.CookieDomain,
                Secure   = FormsAuthentication.RequireSSL,
                Path     = FormsAuthentication.FormsCookiePath,
                Expires  = expires
            };

            HttpContext currentHttp = HttpContext.Current;

            if (currentHttp == null)
            {
                new InvalidOperationException();
            }

            currentHttp.Response.Cookies.Remove(cookie.Name);
            currentHttp.Response.Cookies.Add(cookie);
        }
        public static MyFormPrincipal TryParsePrincipal(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }

            HttpCookie cookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                return(null);
            }

            try
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
                if (ticket == null || string.IsNullOrEmpty(ticket.UserData))
                {
                    return(null);
                }

                MyFormsAuthentication useData = JsonConvert.DeserializeObject <MyFormsAuthentication>(ticket.UserData);
                if (useData == null)
                {
                    return(null);
                }

                return(new MyFormPrincipal(ticket, useData));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        void MvcApplication_AuthorizeRequest(object sender, EventArgs e)
        {
            MyFormPrincipal principal = MyFormsAuthentication.TryParsePrincipal(this.Context);

            if (principal == null || principal.UserData == null)
            {
                return;
            }

            this.Context.User = principal;
        }
        public MyFormPrincipal(FormsAuthenticationTicket ticket, MyFormsAuthentication userData)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            if (userData == null)
            {
                throw new ArgumentNullException("userData");
            }

            this.Identity = new FormsIdentity(ticket);
            this.UserData = userData;
        }