HttpCookie クラス

定義

個々の HTTP クッキーの作成および操作のタイプ セーフな方法を提供します。

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

次のコード例は、オブジェクトで名前が付けられた DateCookieExample Cookie を確認する方法を HttpRequest 示しています。 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 には、2 つの組み込み Cookie コレクションが含まれています。 オブジェクトのHttpRequestコレクションをCookies介してアクセスされるコレクションには、クライアントによってヘッダー内のサーバーに送信される Cookie がCookie含まれています。 オブジェクトのHttpResponseコレクションをCookies介してアクセスされるコレクションには、サーバーで作成され、HTTP 応答ヘッダーでクライアントに送信される新しい Cookie がSet-Cookie含まれています。

コンストラクター

HttpCookie(String)

新しいクッキーを作成し、名前を付けます。

HttpCookie(String, String)

新しいクッキーを作成し、名前を付け、値を割り当てます。

プロパティ

Domain

クッキーを関連付けるドメインを取得または設定します。

Expires

クッキーの有効期限の日時を取得または設定します。

HasKeys

クッキーにサブキーがあるかどうかを示す値を取得します。

HttpOnly

クライアント側のスクリプトでクッキーにアクセスできるかどうかを示す値を取得または設定します。

Item[String]

Values プロパティへのショートカットを取得します。 このプロパティは、Active Server Pages (ASP) の旧バージョンとの互換性のために用意されています。

Name

クッキーの名前を取得または設定します。

Path

現在のクッキーを使用して送信する仮想パスを取得または設定します。

SameSite

Cookie の SameSite 属性の値を取得または設定します。

Secure

SSL (Secure Sockets Layer) を使用して (つまり、HTTPS 経由だけで) クッキーを送信するかどうかを示す値を取得または設定します。

Shareable

クッキーが出力キャッシュに参加を許可されているかどうかを判定します。

Value

各クッキーの値を取得または設定します。

Values

1 つのクッキー オブジェクト内に格納されているキー/値ペアのコレクションを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
TryParse(String, HttpCookie)

指定した文字列形式の Cookie を、等価の HttpCookie に変換し、変換に成功したかどうかを示す値を返します。

適用対象

こちらもご覧ください