学习无止境!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#中Cookies的存取 c#中cookies的存取操作
c#中cookies的存取
cookies的创建:

在客户端创建一个username的cookies,其值为gjy,有效期为1天.
方法1:
Response.Cookies["username"].Value="zxf";
Response.Cookies["username"].Expires=DateTime.Now.AddDays(1);

方法2:
System.Web.HttpCookie newcookie=new HttpCookie("username");
newcookie.Value="gjy";
newcookie.Expires=DateTime.Now.AddDays(1);
Response.AppendCookie(newcookie);


创建带有子键的cookies:
System.Web.HttpCookie newcookie=new HttpCookie("user");
newcookie.Values["username"]="zxf";
newcookie.Values["password"]="111";
newcookie.Expires=DateTime.Now.AddDays(1);
Response.AppendCookie(newcookie);

 

cookies的读取:

无子键读取:
if(Request.Cookies["username"]!=null)
{
Response.Write(Server.HtmlEncode(Request.Cookies["username"].Value));
}

有子键读取:
if(Request.Cookies["user"]!=null)
{
Response.Write(Server.HtmlEncode(Request.Cookies["user"]["username"].Value));
Response.Write(Server.HtmlEncode(Request.Cookies["user"]["password"].Value));

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class Cookie
{
    ///


    /// Cookies赋值
    ///
    /// 主键
    /// 键值
    /// 有效天数
    ///
    public bool setCookie(string strName, string strValue, int strDay)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
            //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            Cookie.Expires = DateTime.Now.AddDays(strDay);
            Cookie.Value = strValue;
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 读取Cookies
    ///
    /// 主键
    ///

    public string getCookie(string strName)
    {
        HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
        if (Cookie != null)
        {
            return Cookie.Value.ToString();
        }
        else
        {
            return null;
        }
    }

    ///


    /// 删除Cookies
    ///
    /// 主键
    ///
    public bool delCookie(string strName)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
            //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            Cookie.Expires = DateTime.Now.AddDays(-1);
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}


示例:
Cookie Cookie = new Cookie();
Cookie.setCookie("name", "aaa",1);//赋值
Cookie.getCookie("name");//取值
Cookie.delCookie("name");//删除
注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可


另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class Cookie
{
    ///


    /// Cookies赋值
    ///
    /// 主键
    /// 键值
    /// 有效天数
    ///
    public bool setCookie(string strName, string strValue, int strDay)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
            //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            Cookie.Expires = DateTime.Now.AddDays(strDay);
            Cookie.Value = strValue;
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }

    ///


    /// 读取Cookies
    ///
    /// 主键
    ///

    public string getCookie(string strName)
    {
        HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
        if (Cookie != null)
        {
            return Cookie.Value.ToString();
        }
        else
        {
            return null;
        }
    }

    ///


    /// 删除Cookies
    ///
    /// 主键
    ///
    public bool delCookie(string strName)
    {
        try
        {
            HttpCookie Cookie = new HttpCookie(strName);
            //Cookie.Domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            Cookie.Expires = DateTime.Now.AddDays(-1);
            System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}


示例:
Cookie Cookie = new Cookie();
Cookie.setCookie("name", "aaa",1);//赋值
Cookie.getCookie("name");//取值
Cookie.delCookie("name");//删除
注意:当Cookie存中文出现乱码,则在存放时给中文编码,如Cookie.setCookie("name", Server.UrlEncode("aaa"),1),读取时解码即可


另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效

 

 

 

(二)

用C#如何创建、读取cookie ClickNum:187|ReplyNum:0

写入:
代码如下:
HttpCookie cookie = new HttpCookie("id_admin_");
cookie.Value = model.id_admin_.ToString();
//cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie("name_admin_");
//可能是中文字符,必须经过编码
cookie.Value = HttpUtility.UrlEncode(model.name_admin_);
//cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);
cookie = new HttpCookie("guid");
cookie.Value = Guid.NewGuid().ToString();
//cookie.Domain = ".sosuo8.com";
HttpContext.Current.Response.Cookies.Add(cookie);

读取:
代码如下:
HttpContext.Current.Request.Cookies["guid"].Value

asp.net清空cookie 清空单个
复制代码 代码如下:
Response.Cookies["admin"].Expires = DateTime.Now.AddDays(-1);

asp.net清空cookie 清空所有
Request.Cookies.Clear()这个方法并不是删除Cookie
删除 Cookie(即从用户的硬盘中物理移除 Cookie)是修改 Cookie 的一种形式。
由于 Cookie 在用户的计算机中,因此无法将其直接移除。
但是,可以让浏览器来为您删除 Cookie。
该技术是创建一个与要删除的 Cookie 同名的新 Cookie,
并将该 Cookie 的到期日期设置为早于当前日期的某个日期。
当浏览器检查 Cookie 的到期日期时,浏览器便会丢弃这个现已过期的 Cookie。


下面的代码示例演示删除应用程序中所有可用 Cookie 的一种方法:
代码如下:
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}

 

 

(My)

/// <summary>
/// 登陆
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void IbtnLogin_Click(object sender, ImageClickEventArgs e)
        {
   HttpCookie cookie = new HttpCookie("YS_Userinfo", CommonClass.Security.EncryptDES(loginmod.Id.ToString(), "1shangkj"));
                Response.SetCookie(cookie);
                Response.Redirect("indexUser.aspx");
        }

/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        protected void IbtnGO_Click(object sender, ImageClickEventArgs e)
        {
            if (HttpContext.Current.Response.Cookies["YS_Userinfo"] != null)
            {
                HttpContext.Current.Response.Cookies["YS_Userinfo"].Expires = DateTime.Now.AddDays(-1);// 设置过期
            }
            Response.Redirect("index.aspx");
        }

posted on 2011-02-22 16:35  钻石眼泪  阅读(7796)  评论(0编辑  收藏  举报