예제 #1
0
        public static void CreateTicket(HttpResponseBase response, UserEntity user)
        {
            FormsAuthenticationTicket ticket;
            string userData = CustomPrincipal.MakeTicketUserData(user.Id);

            ticket = new FormsAuthenticationTicket(
                1,
                user.Name,
                DateTime.Now,
                DateTime.Now.AddMinutes(30),
                true,
                userData,
                FormsAuthentication.FormsCookiePath);

            string authTicket = FormsAuthentication.Encrypt(ticket);

            //将加密后的票据保存为cookie
            HttpCookie cookie = response.Cookies[FormsAuthentication.FormsCookieName];

            if (cookie == null)
            {
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authTicket);
            }
            else
            {
                cookie.Value = authTicket;
            }

            cookie.Path     = FormsAuthentication.FormsCookiePath;
            cookie.HttpOnly = false;
            cookie.Secure   = FormsAuthentication.RequireSSL;
            cookie.Path     = FormsAuthentication.FormsCookiePath;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }

            response.Cookies.Remove(FormsAuthentication.FormsCookieName);
            response.Cookies.Add(cookie);
        }