HttpCookie 类

定义

提供创建和操作各 HTTP Cookie 的类型安全方法。Provides a type-safe way to create and manipulate individual HTTP cookies.

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

示例

下面的代码示例演示如何检查对象中名为的 cookie DateCookieExample HttpRequestThe following code example demonstrates how to check for a cookie named DateCookieExample in the HttpRequest object. 如果未找到 cookie,则将创建它并将其添加到 HttpResponse 对象。If the cookie is not found, it is created and added to the HttpResponse object. Cookie 设置为10分钟后过期。The cookie is set to expire in 10 minutes.

<%@ 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 的属性。The HttpCookie class gets and sets properties of individual cookies. HttpCookieCollection类提供用于存储、检索和管理多个 cookie 的方法。The HttpCookieCollection class provides methods to store, retrieve, and manage multiple cookies.

ASP.NET 包括两个内部 cookie 集合。ASP.NET includes two intrinsic cookie collections. 通过对象集合访问的集合 Cookies HttpRequest 包含客户端在标头中传输到服务器的 cookie CookieThe collection accessed through the Cookies collection of the HttpRequest object contains cookies transmitted by the client to the server in the Cookie header. 通过对象集合访问的集合 Cookies HttpResponse 包含在服务器上创建的新 cookie,并将其传输到 Set-Cookie HTTP 响应标头中的客户端。The collection accessed through the Cookies collection of the HttpResponse object contains new cookies created on the server and transmitted to the client in the Set-Cookie HTTP response header.

构造函数

HttpCookie(String)

创建并命名新的 Cookie。Creates and names a new cookie.

HttpCookie(String, String)

创建和命名新的 Cookie,并为其赋值。Creates, names, and assigns a value to a new cookie.

属性

Domain

获取或设置将此 Cookie 与其关联的域。Gets or sets the domain to associate the cookie with.

Expires

获取或设置此 Cookie 的过期日期和时间。Gets or sets the expiration date and time for the cookie.

HasKeys

获取一个值,通过该值指示 Cookie 是否具有子键。Gets a value indicating whether a cookie has subkeys.

HttpOnly

获取或设置一个值,该值指定 Cookie 是否可通过客户端脚本访问。Gets or sets a value that specifies whether a cookie is accessible by client-side script.

Item[String]

获取 Values 属性的快捷方式。Gets a shortcut to the Values property. 此属性是为了与以前的 Active Server Pages (ASP) 版本兼容而提供的。This property is provided for compatibility with previous versions of Active Server Pages (ASP).

Name

获取或设置 Cookie 的名称。Gets or sets the name of a cookie.

Path

获取或设置要与当前 Cookie 一起传输的虚拟路径。Gets or sets the virtual path to transmit with the current cookie.

SameSite

获取或设置 cookie 的 SameSite 属性的值。Gets or sets the value for the SameSite attribute of the cookie.

Secure

获取或设置一个值,该值指示是否使用安全套接字层 (SSL)(即仅通过 HTTPS)传输 Cookie。Gets or sets a value indicating whether to transmit the cookie using Secure Sockets Layer (SSL)--that is, over HTTPS only.

Shareable

确定 cookie 是否允许参与输出缓存。Determines whether the cookie is allowed to participate in output caching.

Value

获取或设置单个 Cookie 值。Gets or sets an individual cookie value.

Values

获取单个 Cookie 对象所包含的键值对的集合。Gets a collection of key/value pairs that are contained within a single cookie object.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

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

将 Cookie 的指定字符串表示形式转换为其 HttpCookie 等效项,并返回一个指示转换是否成功的值。Converts the specified string representation of a cookie into its HttpCookie equivalent and returns a value that indicates whether the conversion succeeded.

适用于

另请参阅