HttpCookie 类

定义

提供一种类型安全的方法来创建和操作单个 HTTP Cookie。

public ref class HttpCookie sealed
public sealed class HttpCookie
type HttpCookie = class
Public NotInheritable Class HttpCookie
继承
HttpCookie

示例

下面的代码示例演示如何检查对象中命名 DateCookieExampleHttpRequest Cookie。 如果未找到 Cookie,则会创建 Cookie 并将其添加到 HttpResponse 对象。 Cookie 设置为在 10 分钟内过期。

<%@ 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();
        // 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>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sb As New StringBuilder()
        ' Get cookie from current request.
        Dim cookie As HttpCookie
        cookie = Request.Cookies.Get("DateCookieExample")
        
        ' Check if cookie exists in the current request
        If (cookie Is Nothing) Then
            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/>")
        End If
        Label1.Text = sb.ToString()

    End Sub
</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>

注解

HttpCookie 类获取和设置各个 Cookie 的属性。 该 HttpCookieCollection 类提供用于存储、检索和管理多个 Cookie 的方法。

ASP.NET 包括两个内部 Cookie 集合。 通过 Cookies 对象集合访问的 HttpRequest 集合包含客户端将 Cookie 传输到标头中的 Cookie 服务器。 通过 Cookies 对象集合访问的 HttpResponse 集合包含服务器上创建并传输到 HTTP 响应标头中的 Set-Cookie 客户端的新 Cookie。

构造函数

名称 说明
HttpCookie(String, String)

创建、命名和向新 Cookie 分配值。

HttpCookie(String)

创建并命名新的 Cookie。

属性

名称 说明
Domain

获取或设置要与 Cookie 关联的域。

Expires

获取或设置 Cookie 的到期日期和时间。

HasKeys

获取一个值,该值指示 Cookie 是否具有子项。

HttpOnly

获取或设置一个值,该值指定 Cookie 是否可供客户端脚本访问。

Item[String]

获取属性的 Values 快捷方式。 提供此属性是为了与以前版本的 Active Server Pages (ASP) 兼容。

Name

获取或设置 Cookie 的名称。

Path

获取或设置使用当前 Cookie 传输的虚拟路径。

SameSite

获取或设置 Cookie 的 SameSite 属性的值。

Secure

获取或设置一个值,该值指示是否使用安全套接字层(SSL)(即仅通过 HTTPS 传输 Cookie)。

Shareable

确定是否允许 Cookie 参与输出缓存。

Value

获取或设置单个 Cookie 值。

Values

获取包含在单个 Cookie 对象中的键/值对的集合。

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
TryParse(String, HttpCookie)

将 Cookie 的指定字符串表示形式转换为等效 HttpCookie 项,并返回一个值,该值指示转换是否成功。

适用于

另请参阅