1 HttpCookie cookie = new HttpCookie("OrderApiCookie"); //初使化并设置Cookie的名称 2 cookie.HttpOnly = true; //为true时不允许脚本文件访问 3 cookie.Domain = ".taobao.com"; //注意domain必须设置为一个点加顶级域名,定义可以访问cookie的域名 4 //DateTime dt = DateTime.Now; 5 //TimeSpan ts = new TimeSpan(0, 1, 1, 0, 0); //过期时间为1分钟 6 //cookie.Expires = dt.Add(ts); //设置过期时间,不设置时间浏览器关闭会销毁 7 cookie.Value = ".net"; //给cookie赋值 8 Response.AppendCookie(cookie); //返回给浏览器
cookie.Domain 为允许访问的域名,这里设置的.taobao.com,那么非 *.taobao.com的网站都不能访问
cookie.HttpOnly = true 时,前台就获取不到cookie值了
1 function getCookie(cookieName) { 2 var arr, reg = new RegExp("(^| )" + cookieName + "=([^;]*)(;|$)"); 3 if (arr = document.cookie.match(reg)) 4 alert(unescape(arr[2])); 5 else 6 alert("null"); 7 }