HttpCookie 類別

定義

提供型別安全 (Type-Safe) 方式來建立並操作個別 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 集合包含伺服器上建立的新 Cookie,並傳輸至 HTTP 回應標頭中的 Set-Cookie 用戶端。

建構函式

HttpCookie(String)

建立並命名新 Cookie。

HttpCookie(String, String)

建立、命名和指派數值給新 Cookie。

屬性

Domain

取得或設定與 Cookie 關聯的網域。

Expires

取得或設定 Cookie 的到期日和時間。

HasKeys

取得值,指出 Cookie 是否有子索引鍵 (Subkey)。

HttpOnly

取得或設定數值,指定 Cookie 是否由用戶端指令碼存取。

Item[String]

取得 Values 屬性的捷徑。 提供這個屬性來與 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,並傳回一個值指出轉換是否成功。

適用於

另請參閱