`
vtrtbb
  • 浏览: 354223 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

同一IP下多端口网站共享cookie的问题

 
阅读更多

以前开开发过程中使用cookie比较随便,后来发现同一个cookie在不同端口的站点下面其实是共有的,给开发造成一定的麻烦。现在我的cookie访问类已经修改过来了。所以说,有时候网上的东西还是不能拿过来就用啊,具体问题还得具体分析

 

 

 

using System;
using System.Web;
namespace Common.Web
{
    /// <summary>
    /// 操作Cookie
    /// </summary>
    public class Cookie
    {

        /// <summary>
        /// 创建Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <param name="strValue">Cookie 键值</param>
        /// <param name="strDay">Cookie 天数(单位:30分钟)</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.setCookie("主键","键值","天数");</code>
        public bool SetCookie(string strName, string strValue, int strDay)
        {
            try
            {
                DelCookie(strName);
                string port = HttpContext.Current.Request.Url.Port.ToString();
                strName = strName + "_" + port;
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(strDay / 48.0);
                //  Cookie.Expires = DateTime.Now.AddDays(strDay / 1.0);
                Cookie.Value = strValue;
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                //     Common.LogManager.GetInstance().Log("设置COOKIE失败");
                return false;
            }
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.getCookie("主键");</code>
        public string GetCookie(string strName)
        {
            string port = HttpContext.Current.Request.Url.Port.ToString();
            strName = strName + "_" + port;
            HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
            if (Cookie != null)
            {
                if (Cookie.Value != null)
                {
                    return Cookie.Value.ToString();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除Cookies
        /// </summary>
        /// <param name="strName">Cookie 主键</param>
        /// <code>Cookie ck = new Cookie();</code>
        /// <code>ck.delCookie("主键");</code>
        public bool DelCookie(string strName)
        {
            try
            {
                string port = HttpContext.Current.Request.Url.Port.ToString();
                strName = strName + "_" + port;
                HttpCookie Cookie = new HttpCookie(strName);
                Cookie.Expires = DateTime.Now.AddDays(-100);
                Cookie.Value = null;
                System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 得到网站的Cookie
        /// </summary>
        /// <param name="url">网站域名 如http://www.xxx.com</param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public System.Net.CookieCollection GetCookieCollection(string url, System.Net.CookieContainer cookie)
        {
            Uri uri = new Uri(url);
            System.Net.CookieCollection ccll = cookie.GetCookies(uri);
            return ccll;
        }

    }
}

 

function getcookie(varName) {
    var port = location.port; //浏览器端口
    if (port == "") {
        port = "80";
    }
    var cookieStr = document.cookie;
    if (cookieStr == "") {
        return "0";
    }
    var cookieValue = cookieStr.split(";");
    //alert(cookieValue);
    var startPos = -1;
    var endPos = -1;
    for (var i = 0; i < cookieValue.length; i++) {
        //startPos = cookieValue[i].indexOf(varName);
        var key = cookieValue[i].substring(0, cookieValue[i].indexOf("="))
        //alert(("查询到的KEY " + key).toString().length);
        //alert(("希望查询到的KEY " + varName + "_" + port).toString().length);
        if (key.replace(" ", "") == (varName + "_" + port).replace(" ", "")) {
            startPos = cookieValue[i].indexOf("=")+1
            var css = cookieValue[i].substring(startPos);
            //alert(varName + ":" + port + "=" + css);
                //alert("查询 "+varName+" 得到 "+css);
            return css;
        }
    }

    return "0";
}
function savecookie(key, val) {
    var port = location.port;
    if (port == "") {
        port = "80";
    }
    var the_date = new Date("December 31, 2020");
    var expiresDate = the_date.toGMTString();
    document.cookie = key + "_" + port + "=" + escape(val) + "; expires=" + expiresDate;
}
function delCookie(name) {
    var port = location.port;
    if (port == "") {
        port = "80";
    }
    var exp = new Date();
    exp.setTime(exp.getTime() - 1000);
    var cval = getcookie(name);
    document.cookie = name + "_" + port + "=" + escape(cval) + ";expires=" + exp.toGMTString();
}
function getPort(str) {
    s = str.split('_');
    return s[1];
}

 

来自http://www.cnblogs.com/zhuzhenyu/archive/2012/07/27/2611958.html

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics