HttpCookie 類別

定義

提供一種型別安全的方式來建立和操作個別 HTTP Cookie。

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

範例

以下程式碼範例示範如何檢查物件中命名DateCookieExampleHttpRequest的 cookie。 如果找不到 cookie,則會建立它並加入 HttpResponse 物件。 餅乾設定在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。

建構函式

名稱 Description
HttpCookie(String, String)

建立、命名並指派一個新的 Cookie 值。

HttpCookie(String)

建立並命名一個新的 cookie。

屬性

名稱 Description
Domain

取得或設定網域來關聯 cookie。

Expires

取得或設定餅乾的有效期限和時間。

HasKeys

會獲得一個值,表示 Cookie 是否有子鍵。

HttpOnly

取得或設定一個值,指定 cookie 是否能被用戶端腳本存取。

Item[String]

有捷徑到那 Values 個地產。 此特性是為了與先前版本的 Active Server Pages(ASP)相容性而提供。

Name

取得或設定餅乾名稱。

Path

取得或設定虛擬路徑以目前的 Cookie 傳輸。

SameSite

取得或設定 Cookie 的 SameSite 屬性值。

Secure

會取得或設定一個值,指示是否使用安全套接字層(SSL)傳輸 Cookie——即僅透過 HTTPS 傳輸。

Shareable

判斷 Cookie 是否被允許參與輸出快取。

Value

取得或設定一個獨立的 Cookie 值。

Values

會取得一組包含在單一 Cookie 物件中的鍵值對。

方法

名稱 Description
Equals(Object)

判斷指定的 物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
TryParse(String, HttpCookie)

將指定的 cookie 字串表示轉換為其 HttpCookie 對應的,並回傳一個表示轉換是否成功的值。

適用於

另請參閱