您的位置:首页 > 编程语言 > C#

C#cookies写入,读取事例

2007-08-17 09:11 302 查看
//写入

string ip = Request.UserHostAddress;
HttpCookie cookie = new HttpCookie("CookieName");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("name");
sb.Append("#");
sb.Append(id.ToString());
sb.Append("#");
Random rnd = new Random();
int type = rnd.Next(0,9999);
cookie.Values.Add("Value",type.ToString());
cookie.Values.Add("Text",type.ToString());
cookie.Expires = DateTime.MaxValue;
Response.AppendCookie(cookie);

//读取

HttpCookie cookie = Request.Cookies["CookieName"];
int type = int.Parse(cookie.Values["Value"].ToString());
string cookieStr = Functions.DecryptCookie(cookie.Values["Text"].ToString(),type);
string [] myCookie = new String[10];
myCookie = cookieStr.Split('#');
name = myCookie[0];
string id = myCookie[1];
string now = myCookie[2];
string times = myCookie[3];
string ip = myCookie[4];
string seal = myCookie[5];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: