本帖最后由 qiudong_5210 于 2011-02-14 17:33:27 编辑

解决方案 »

  1.   

    .NET 操作COOKIEcookie使用方法如下:
    启用cookie:
    Response.Cookies.Add(new HttpCookie("CheckCode", "95s8t"));
    // 第一个参数是cookie的名字 第二个是值
    再给你一段js获取cookie值的代码:
    // 参数是cookie名称 返回对应cookie名称的值   如果不存在返回null
    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;
        }
      

  2.   

    function SetCookie(name,value)
    {
      var Days = 30;   
      var exp = new Date();   
      exp.setTime(exp.getTime() + Days*24*60*60*1000);
      document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    function getCookie(name)   
    {
      var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
      if(arr != null) return unescape(arr[2]); return null;
    }c# 
    static public void Add(string strName, string strValue, int strMinute)
      {
      try
      {
      HttpCookie Cookie = new HttpCookie(DESCrypt.Crypt(strName));
      Cookie.Expires = DateTime.Now.AddMinutes(strMinute);
      Cookie.Value = DESCrypt.Crypt(strValue);
      System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
      }
      catch(Exception ex)
      {
      throw new Exception(ex.Message);
      }
      }
      static public string Read(string strName)
      {
      HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[DESCrypt.Crypt(strName)];
      if (Cookie != null)
      {
      return DESCrypt.DeCrypt(Cookie.Value);
      }
      else
      {
      return "";
      }
      }  static public void Delete(string strName)
      {
      try
      {
      HttpCookie Cookie = new HttpCookie(DESCrypt.Crypt(strName));
      Cookie.Expires = DateTime.Now.AddDays(-1);
      System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
      }
      catch (Exception ex)
      {
      throw new Exception(ex.Message);
      }
      }  static public bool Exists(string strName)
      {  
      bool b=false;
      if (Read(strName) != null && Read(strName) != "")
      {
      b = true;
      }
      return b;
      }