日期:2014-05-17  浏览次数:20372 次

Cookie存取问题
获取
C# code

HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo");
                //有cookie值的话就取出来
                if (Cookie != null)
                {
                    this.txtUserName.Value = HttpUtility.UrlDecode(Cookie.Values["uName"]);
                    this.txtPwd.Attributes.Add("value", Cookie.Values["Pwd"]);
                }


登录 保存与不保存问题!(记住我 复选框 有没有选择)
C# code

// 是否选中复选框(默认为true)
                            if (this.chk.Checked == true)
                            {
                                //获得cookie
                                HttpCookie Cookie = CookiesHelper.GetCookie("UserInfo");
                                //string cooke = HttpUtility.UrlDecode(CookiesHelper.GetCookie("UserInfo").Values["uName"]);
                                if (Cookie == null)
                                {
                                    Cookie = new HttpCookie("UserInfo");
                                    Cookie.Values.Add("uName", HttpUtility.UrlEncode(uName));
                                    Cookie.Values.Add("Pwd", Pwd);
                                    //设置Cookie过期时间
                                    Cookie.Expires = DateTime.Now.AddDays(2);
                                    CookiesHelper.AddCookie(Cookie);
                                }
                                else if (!Cookie.Values["uName"].Equals(uName))
                                {
                                    CookiesHelper.SetCookie("UserInfo", "uName", uName);
                                }
                                Response.Redirect("~/Menu/Index_Menu.htm");


cookie类
C# code

/// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        public static void AddCookie(HttpCookie cookie)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                //指定客户端脚本是否可以访问[默认为false]
                cookie.HttpOnly = true;
                //指定统一的Path,比便能通存通取
                cookie.Path = "/";
                //设置跨域,这样在其它二级域名下就都可以访问到了
                //cookie.Domain = "chinesecoo.com";
                response.AppendCookie(cookie);
            }
        }
/// <summary>
        /// 设置Cookie子键的值
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie(string cookieName, string key, string value)
        {
            SetCookie(cookieName, key, value, null);
        }
/// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expires"></param>
        public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                HttpCookie cookie = response.Cookies[cookieName];
                if (cookie != null)
                {
                    if (!string.I