HttpCookie 类:提供创建和操作各 HTTP Cookie 的类型安全方法。
下面的代码示例演示如何对 HttpRequest 对象中名为 DateCookieExample 的 Cookie 进行检查。如果找不到该 Cookie,则将创建它并将其添加到 HttpResponse 对象。Cookie 设置为 10 分钟后过期。
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Get cookie from the current request.
HttpCookie cookie = Request.Cookies.Get("DateCookieExample");
// Check if cookie exists in the current request.
if (cookie == null)
{
sb.Append("Cookie was not received from the client. ");
sb.Append("Creating cookie to add to the response. <br/>");
// Create cookie.
cookie = new HttpCookie("DateCookieExample");
// Set value of cookie to current date time.
cookie.Value = DateTime.Now.ToString();
// Set cookie to expire in 10 minutes.
cookie.Expires = DateTime.Now.AddMinutes(10d);
// Insert the cookie in the current HttpResponse.
Response.Cookies.Add(cookie);
}
else
{
sb.Append("Cookie retrieved from client. <br/>");
sb.Append("Cookie Name: " + cookie.Name + "<br/>");
sb.Append("Cookie Value: " + cookie.Value + "<br/>");
sb.Append("Cookie Expiration Date: " +
cookie.Expires.ToString() + "<br/>");
}
Label1.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookie Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
下面的代码示例演示如何使用 HttpRequest 对象的 Cookies 属性读取 Cookie 以及如何使用 HttpResponse 对象的 Cookies 属性写入 Cookie。这两个属性均返回 HttpCookieCollection 对象。如果两个 Cookie 中的任何一个命名为 userName,且 lastVisit 不在 HTTP 请求中,则在 HTTP 响应中创建这两个 Cookie。如果两个 Cookie 存在,则显示这两个 Cookie 的属性。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Check to see if cookies exist in HTTP request.
if (Request.Cookies["userName"] == null &&
Request.Cookies["lastVist"] == null)
{
Response.Cookies["userName"].Value = "user name";
Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(20d);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddMinutes(20d);
Response.Cookies.Add(aCookie);
sb.Append("Two cookies added to response. " +
"Refresh the page to read the cookies.");
}
else
{
HttpCookieCollection cookies = Request.Cookies;
for (int i = 0; i < cookies.Count; i++)
{
sb.Append("Name: " + cookies[i].Name + "<br/>");
sb.Append("Value: " + cookies[i].Value + "<br/>");
sb.Append("Expires: " + cookies[i].Expires.ToString() +
"<br/><br/>");
}
}
Label1.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookieCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>