Esempio n. 1
0
        /// <summary>
        /// 将会员信息保存到cookie
        /// </summary>
        /// <param name="model"></param>
        public static void SetCookie(YLPMember model)
        {
            /*
             * Forms验证在内部的机制为把用户数据加密后保存在一个基于cookie的票据FormsAuthenticationTicket中,因为是经过特殊加密的,所以应该来说是比较安全的。
             * 而.net除了用这个票据存放自己的信息外,还留了一个地给用户自由支配,这就是现在要说的UserData。
             *
             */

            var expires = DateTime.Now.AddMinutes(30);

            if (model.Enduring)
                expires = DateTime.Now.AddDays(7);

            //创建一个新的票据,将客户ip记入ticket的userdata
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1,//int version,
                model.Id,//string name,
                DateTime.Now,//DateTime issueDate,
                expires,//DateTime expiration,
                false,//bool isPersistent,
                string.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}", SPLIT, model.Id, model.Role, model.Account, model.Name, model.Enduring)//string userData
                );

            //将票据加密
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            //将加密后的票据存入cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.Domain = FormsAuthentication.CookieDomain;
            if (model.Enduring)
                cookie.Expires = expires;

            HttpContext.Current.Response.Cookies.Add(cookie);
        }
Esempio n. 2
0
        /// <summary>
        /// 将会员信息保存到cookie
        /// </summary>
        /// <param name="model"></param>
        public static void SetCookie(YLPMember model)
        {
            /*
             * Forms验证在内部的机制为把用户数据加密后保存在一个基于cookie的票据FormsAuthenticationTicket中,因为是经过特殊加密的,所以应该来说是比较安全的。
             * 而.net除了用这个票据存放自己的信息外,还留了一个地给用户自由支配,这就是现在要说的UserData。
             *
             */

            var expires = DateTime.Now.AddMinutes(30);

            if (model.Enduring)
            {
                expires = DateTime.Now.AddDays(7);
            }

            //创建一个新的票据,将客户ip记入ticket的userdata
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1,                                                                                                                   //int version,
                model.Id,                                                                                                            //string name,
                DateTime.Now,                                                                                                        //DateTime issueDate,
                expires,                                                                                                             //DateTime expiration,
                false,                                                                                                               //bool isPersistent,
                string.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}", SPLIT, model.Id, model.Role, model.Account, model.Name, model.Enduring) //string userData
                );

            //将票据加密
            string encryptedTicket = FormsAuthentication.Encrypt(ticket);

            //将加密后的票据存入cookie
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            cookie.Domain = FormsAuthentication.CookieDomain;
            if (model.Enduring)
            {
                cookie.Expires = expires;
            }

            HttpContext.Current.Response.Cookies.Add(cookie);
        }