asp.net C#中Cookies的存取代码

作者:袖梨 2022-06-25

asp教程.net c#中cookies的存取代码

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

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),读取时解码即可

相关文章

精彩推荐