asp.net js 存取cookie

 

 

asp.net

 

//传进来的 public BaseController(BaseHttpHandler handler, HttpContext context)
//        { 


//根据地址设置cookie
            string CityName = "";
            if (context.Request.Cookies["_AreaKey"] == null || context.Request.Cookies["_AreaValue"] == null)
            {
                var companyID = ConvertHelper.ToInt(context.Request.QueryString["CompanyID"]);
                HttpCookie CityCookieKey = new HttpCookie("_AreaKey");
                HttpCookie CityCookieValue = new HttpCookie("_AreaValue");
                HttpCookie CityCookieName = new HttpCookie("_AreaName");

                if (companyID == 3)
                {
                    CityCookieKey.Value = "FZ";
                    CityCookieValue.Value = "3";
                    CityName = "福州";
                    CityCookieName.Value = CityName;
                }
                else if (companyID == 4)
                {
                    CityCookieKey.Value = "XM";
                    CityCookieValue.Value = "4";
                    CityName = "厦门";
                    CityCookieName.Value = CityName;

                }
                else
                {
                    //获取分配中心地址 跳转
                    context.Response.Redirect(ConfigurationManager.AppSettings["WebCenter"]);
                }
                CityCookieKey.Expires = DateTime.Now.AddDays(1);
                CityCookieValue.Expires = DateTime.Now.AddDays(1);
                CityCookieName.Expires = DateTime.Now.AddDays(1);
                CityCookieKey.Path = "/";
                CityCookieValue.Path = "/";
                CityCookieName.Path = "/";
                context.Response.Cookies.Add(CityCookieKey);
                context.Response.Cookies.Add(CityCookieValue);
                context.Response.Cookies.Add(CityCookieName);
            }
            else
            {
                var CityValue = ConvertHelper.ToInt(context.Request.Cookies["_AreaValue"].Value);

                if (CityValue == 3)
                {
                    CityName = "福州";
                }
                if (CityValue == 4)
                {
                    CityName = "厦门";
                }
            }
View Code

如果是顶级域名下的二级域名跨域  可以设置cookie的domain 

 

js

//写cookies
function setCookie(name, value,time) {
    var Days = time||365;
    var exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString()+";path=/;";
}
//读取cookies
function getCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}

//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() + -1 * 24 * 60 * 60 * 1000);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=;expires=" + exp.toGMTString() + "; path=/;";
}
//清楚所有cookie
function clareCookie() {

    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; "); // 将多cookie切割为多个名/值对
    for (var i = 0; i < arrCookie.length; i++) { // 遍历cookie数组,处理每个cookie对
        var arr = arrCookie[i].split("=");
        if (arr.length > 0)
            delCookie(arr[0]);
    }
}
View Code

 

传智demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CZBK.ItcastProject.WebApp._2015_5_31
{
    public partial class CookieDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Cookie:是一个客户端状态保持机制,(网站的数据是存在客户端),
            //与隐藏域与ViewState对象都属于这种客户端状态保持,Cookie中存储的是关于网站相关的文本字符串数据。
            //Cookie的存储方式有两种,如果不指定过期时间,那么存储在客户端浏览器内存中,如果指定了过期时间,
            //那么存储在客户端的磁盘上。Cookie是与具体的网站有关的,如果我们将Cookie设置了过期时间,
            //那么当用户在指定时间内访问我们的网站,那么属于我们网站的Cookie数据会放在请求报文中发送过来,
            //其它网站的Cookie不会发送。

            //创建Cookie.
           // Response.Cookies["cp1"].Value = "itcast";


            //创建Cookie并且指定过期时间.
            //Response.Cookies["cp2"].Value = "laowang";
            //Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(3);

            //删除Cookie
           // Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1);


            //Cookie跨域(域名)
            Response.Cookies["cp3"].Value = "laowang";
           //主域名的cookie会自动分享给子域名,但是子域名的不会,所以在这里设置主域名,就实现跨域了
           // Response.Cookies["cp3"].Domain = "xxx.com";//设置主域的。
            Response.Cookies["cp3"].Path = "/2015-5-31";
            Response.Cookies["cp3"].Expires = DateTime.Now.AddDays(3);


            //另外一种创建Cookie的方式。
            HttpCookie cookie1 = new HttpCookie("cp4","sssss");
            cookie1.Expires = DateTime.Now.AddDays(3);
            Response.Cookies.Add(cookie1);
            
        }
    }
}
View Code

 cookie 限制  4K  20个浏览器对每个网站存储的限制   300个  有些浏览器对本身存储总数的限制

posted @ 2016-04-13 16:13  xiaoshi657  阅读(401)  评论(0编辑  收藏  举报