.NET 添加/更新Cookie

来源:互联网 发布:java高级编程 jdk7版 编辑:程序博客网 时间:2024/05/16 18:52

 public static void AddUserCookies(string key,string value, string cookiename, string domain)( I8 W3 {4 V% K2 X! Y( m
{; m0 @0 [: C- y$ U
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cookiename];+ E( }4 A8 o; n
if (cookie == null)
{, y/ E# _( W; n2 A$ v7 h
cookie = new HttpCookie(cookiename);
cookie.Domain = domain;
cookie.Path = _cookiepath;

 cookie.Values.Add(key, value);) K' F0 Q' B# R. V' d
HttpContext.Current.Response.AppendCookie(cookie);2 @! N; U% q* }9 e% H2 i
}
else X$ N3 b: w9 x) P; S
{
if (System.Web.HttpContext.Current.Request.Cookies[cookiename].Values[key] != null)3 w5 b$ `1 R5 U. |7 @4 J T! M) O
{1 p1 p r& e5 O! [6 Q7 |! b6 h8 y q
cookie.Values.Set(key, value);
 }
 else0 Q. D4 s1 n C1 R: B+ L
{, z$ L& ^5 k! F# v4 w# G! C
cookie.Domain = domain;
cookie.Path = _cookiepath;3 j& N8 W: Q& }
cookie.Values.Add(key, value);; q2 q) E. K$ [4 j
HttpContext.Current.Response.AppendCookie(cookie);+ S, o8 |( `6 q2 v" W: E9 w7 A
}
}0 u! l0 b% Q. E) } a* q
}

原创粉丝点击