C#.net 多级cookie写入读取代码,一级、二级域名

//读取双层的
string[,] dd = new string[0,0];
Response.Write(V.mycookie_twoTime("SONHOOCOOKIE", "sonhoogsid",dd , "read", 1));

//设置双层COOKIE
    string[,] sonhoocookie = new string[3, 2];
    sonhoocookie[0, 0] = "sonhoogsid";//变量名
    sonhoocookie[0, 1] = userid;//变量值
    sonhoocookie[1, 0] = "sonhooqymc";//变量2名
    sonhoocookie[1, 1] = "华和兴创想科技";//变量2值
<pre name="code" class="csharp">    sonhoocookie[2, 0] = "sonhooqymc2";
    sonhoocookie[2, 1] = "华和兴创想科技";
V.mycookie_twoTime("SONHOOCOOKIE", "", sonhoocookie, "set", 1);

/**
    *
    *操作cookie值  双层的
    *bianlian1 ,当COOKIE有主域和子域时则用|分隔,主域在前,子域在后
    *当type=read读取 readBianlian,当type=set,设置bianlian2[0][0]为变量名,bianlian2[0][1]变量值, days 以日为单位
    *
    */
    public static string mycookie_twoTime(string bianlian1, string readBianlian, string[,] bianlian2, string type, int days)
    {
        string mycookiea = "";
        HttpCookie mycookie_v = null;
        if (type.ToLower() == "read")
        {//读取该COOKIE的值
            //System.Web.HttpContext.Current.Response.Cookies[bianlian1].Domain = "sonhoo.com";                
            HttpCookie mycookie = null;

            mycookie = System.Web.HttpContext.Current.Request.Cookies[bianlian1];
            if (mycookie != null)
                mycookiea = mycookie[readBianlian];
            return mycookiea;
        }
        else
        {//设置该COOKIE的值
            System.Web.HttpContext.Current.Response.Cookies[bianlian1].Domain = "sonhoo.com";
            DateTime dt = DateTime.Now;  //需要,得到当前时间

            mycookie_v = new HttpCookie(bianlian1);
            for (int i = 0; i < bianlian2.GetLength(0); i++)
            {
                string valuet = System.Web.HttpUtility.UrlEncode(bianlian2[i,1]);
                mycookie_v.Values.Add(bianlian2[i, 0], valuet);
            }
            mycookie_v.Domain = "sonhoo.com";

            mycookie_v.Expires = Convert.ToDateTime(dt + TimeSpan.FromDays(days)); //设定过期时间为1年
            System.Web.HttpContext.Current.Response.Cookies.Add(mycookie_v); //写入COOKIE
            return "";
        }

    }



//操作单层cookie

/**
         *
         *操作cookie值
         *当type=read读取 bianlian,当type=set,设置bianlian
         */
    public static string mycookie(string bianlian, string valuet, string type)
    {
        
        if (type.ToLower() == "read")
        {
            HttpCookie mycookie = System.Web.HttpContext.Current.Request.Cookies[bianlian];

            string mycookiea = "";
            if (mycookie == null)
            { mycookiea = ""; }
            else
            {
                mycookiea = mycookie.Value;
                mycookiea = System.Web.HttpUtility.UrlDecode(mycookiea);
            }
            return mycookiea;

        }
        else
        {
            valuet = System.Web.HttpUtility.UrlEncode(valuet);
            DateTime dt = DateTime.Now;  //需要,得到当前时间
            HttpCookie mycookie = new HttpCookie(bianlian); //申明新的COOKIE变量
            mycookie.Value = valuet; //赋值
            mycookie.Expires = Convert.ToDateTime(dt + TimeSpan.FromDays(365)); //设定过期时间为1年
            System.Web.HttpContext.Current.Response.Cookies.Add(mycookie); //写入COOKIE
            return valuet;
        }

    }



 
   

你可能感兴趣的:(C#.net 多级cookie写入读取代码,一级、二级域名)