HttpCookie クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
個々の HTTP Cookie を作成および操作するためのタイプ セーフな方法を提供します。
public ref class HttpCookie sealed
public sealed class HttpCookie
type HttpCookie = class
Public NotInheritable Class HttpCookie
- 継承
-
HttpCookie
例
次のコード例では、HttpRequest オブジェクトで DateCookieExample という名前の 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 には、2 つの組み込み Cookie コレクションが含まれています。
HttpRequest オブジェクトのCookies コレクションを介してアクセスされるコレクションには、Cookie ヘッダー内のサーバーにクライアントによって送信される Cookie が含まれています。
HttpResponse オブジェクトのCookies コレクションからアクセスされるコレクションには、サーバーで作成され、Set-Cookie HTTP 応答ヘッダーでクライアントに送信される新しい 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 |
Secure Sockets Layer (SSL) を使用して Cookie を送信するかどうかを示す値を取得または設定します。つまり、HTTPS 経由でのみ送信されます。 |
| Shareable |
Cookie が出力キャッシュに参加できるかどうかを判断します。 |
| Value |
個々の Cookie 値を取得または設定します。 |
| Values |
1 つの Cookie オブジェクト内に含まれるキーと値のペアのコレクションを取得します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
| TryParse(String, HttpCookie) |
指定した Cookie の文字列形式を同等の HttpCookie に変換し、変換が成功したかどうかを示す値を返します。 |