cookie在.net中的使用

1.创建一个新的cookie,并赋值。

HttpCookie cookie;
cookie=new HttpCookie("user");
cookie.Domain = AppConfig.DomainName;
cookie.Values.Add("Username",username);
cookie.Values.Add("ldapStr",FindUserlist.Table.Rows[0]["ldapStr"].ToString());
Response.AppendCookie(cookie);

2.获取cookie的值
HttpCookie cookie=Request.Cookies["user"];
if(cookie!=null)
{
string username = cookie["Username"];
string ldapStr = cookie["ldapStr"];
}

------------------------------------------------------------------------------------------------------------

1、写Cookie

HttpCookie myCookie = new HttpCookie("CookieName");
myCookie.Values.Add("CookieItem1","CookieItem1Value");
myCookie.Values.Add("CookieItem2","CookieItem2Value");
myCookie.Expires = DateTime.Now.AddDays(30);
Response.AppendCookie(myCookie);
2、清除Cookie

HttpCookie myCookie = HttpContext.Current.Response.Cookies["CookieObjectName"];
if(myCookie != null)
myCookie.Expires = DateTime.Now;

你可能感兴趣的:(.net,String,user,null)