2006-11-20
Asp.Net2.0 Cookie的應用
'判斷cookie是否存在
If IsNothing(HttpContext.Current.Request.Cookies("CookieName"))
'新增 Cookie 至 Response Cookies 集合中。下列範例顯示完成此工作的兩個方法。
Response.Cookies("userName").Value = "mike"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
Dim aCookie As New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
'建立具有子機碼的 Cookie,可以使用許多撰寫單一 Cookie 的語法。下列範例顯示撰寫相同 Cookie 的兩種方法,各具有兩個子機碼:
Response.Cookies("userInfo")("userName") = "mike"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Dim aCookie As New HttpCookie("userInfo")
aCookie.Values("userName") = "mike"
aCookie.Values("lastVisit") = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
'more
http://www.microsoft.com/taiwan/msdn/library/2003/Mar-2003/vbtchASPNETCookies101.htm
If IsNothing(HttpContext.Current.Request.Cookies("CookieName"))
'新增 Cookie 至 Response Cookies 集合中。下列範例顯示完成此工作的兩個方法。
Response.Cookies("userName").Value = "mike"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
Dim aCookie As New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
'建立具有子機碼的 Cookie,可以使用許多撰寫單一 Cookie 的語法。下列範例顯示撰寫相同 Cookie 的兩種方法,各具有兩個子機碼:
Response.Cookies("userInfo")("userName") = "mike"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Dim aCookie As New HttpCookie("userInfo")
aCookie.Values("userName") = "mike"
aCookie.Values("lastVisit") = DateTime.Now.ToString
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)
'more
http://www.microsoft.com/taiwan/msdn/library/2003/Mar-2003/vbtchASPNETCookies101.htm