HttpCookie.HttpOnly プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > HttpCookie.HttpOnly プロパティの意味・解説 

HttpCookie.HttpOnly プロパティ

メモ : このプロパティは、.NET Framework version 2.0 で新しく追加されたものです。

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

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)
構文構文

Dim instance As HttpCookie
Dim value As Boolean

value = instance.HttpOnly

instance.HttpOnly = value
public bool HttpOnly { get;
 set; }
/** @property */
public boolean get_HttpOnly ()

/** @property */
public void set_HttpOnly (boolean value)

プロパティ値
Cookie に HttpOnly 属性があり、クライアント側のスクリプトではアクセスできない場合は true。それ以外の場合は false。既定値は false です。

解説解説

Microsoft Internet Explorer バージョン 6 Service Pack 1 以降では Cookie のプロパティ HttpOnly をサポートしています。これを使用すると、Cookie の盗難につながるサイト間スクリプトの脅威を軽減できます。盗まれた Cookie には、ASP.NET セッション ID やフォーム認証チケットなど、サイトがユーザーを識別するための機密情報が含まれている場合があり、ユーザーになりすましたり機密情報を取得するため攻撃者に利用される可能性があります。HttpOnly 属性を持つ Cookie を、このプロパティに対応しているブラウザで受け取った場合、クライアント側のスクリプトからその Cookie にアクセスすることはできません。

注意に関するメモ注意

HttpOnly プロパティを true に設定しても、ネットワーク チャネルにアクセスできる攻撃者による Cookie への直接アクセスを防ぐことはできません。このような攻撃からの保護対策には、Secure Sockets Layer (SSL) の使用を検討してください。また、ワークステーションのセキュリティも重要です。悪意のあるユーザーが、開かれたブラウザ ウィンドウや永続的な Cookie が存在するコンピュータを使用して、正規ユーザーの ID で Web サイトにアクセスする可能性があります。

考えられる攻撃の種類、およびこのプロパティを使用した対策の詳細については、「Mitigating Cross-site Scripting With HTTP-only Cookies」を参照してください。

使用例使用例

HttpOnly Cookie の作成方法、およびクライアントから ECMAScript を使用してこれにアクセスできないことを、次のコード例に示します。

<%@ Page Language="VB" %>

<script runat="server">

  Protected Sub Page_Load(ByVal
 sender As Object, ByVal
 e As System.EventArgs)
    
    ' Create a new HttpCookie.
    Dim myHttpCookie As New
 HttpCookie("LastVisit", DateTime.Now.ToString())

    ' By default, the HttpOnly property is set to false 
    ' unless specified otherwise in configuration.

    myHttpCookie.Name = "MyHttpCookie"
    Response.AppendCookie(myHttpCookie)

    ' Show the name of the cookie.
    Response.Write(myHttpCookie.Name)

    ' Create an HttpOnly cookie.
    Dim myHttpOnlyCookie As New
 HttpCookie("LastVisit", DateTime.Now.ToString())

    ' Setting the HttpOnly value to true, makes
    ' this cookie accessible only to ASP.NET.

    myHttpOnlyCookie.HttpOnly = True
    myHttpOnlyCookie.Name = "MyHttpOnlyCookie"
    Response.AppendCookie(myHttpOnlyCookie)

    ' Show the name of the HttpOnly cookie.
    Response.Write(myHttpOnlyCookie.Name)

  End Sub
  
</script>

<html>
<body>
<script>
function getCookie(NameOfCookie)
{
  if (document.cookie.length > 0) 
  { 
    begin = document.cookie.indexOf(NameOfCookie+"=");
 
    if (begin != -1)
    { 
    begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";",
 begin);
      if (end == -1) end
 = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
       
    } 
  }
  return null;  
}
</script>

<script>

// This code returns the cookie name.
alert("Getting HTTP Cookie");
alert(getCookie("MyHttpCookie"));

// Because the cookie is set to
 HttpOnly,
// this returns null.
alert("Getting HTTP Only Cookie");
alert(getCookie("MyHttpOnlyCookie"));

</script> 

</body>
</html>

<%@ Page Language="C#" %>


<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        // Create a new HttpCookie.
        HttpCookie myHttpCookie = new HttpCookie("LastVisit",
 DateTime.Now.ToString());

        // By default, the HttpOnly property is set to false 
        // unless specified otherwise in configuration.

        myHttpCookie.Name = "MyHttpCookie";
        Response.AppendCookie(myHttpCookie);

        // Show the name of the cookie.
        Response.Write(myHttpCookie.Name);

        // Create an HttpOnly cookie.
        HttpCookie myHttpOnlyCookie = new HttpCookie("LastVisit",
 DateTime.Now.ToString());

        // Setting the HttpOnly value to true, makes
        // this cookie accessible only to ASP.NET.

        myHttpOnlyCookie.HttpOnly = true;
        myHttpOnlyCookie.Name = "MyHttpOnlyCookie";
        Response.AppendCookie(myHttpOnlyCookie);

        // Show the name of the HttpOnly cookie.
        Response.Write(myHttpOnlyCookie.Name);
    }
</script>


<html>
<body>
<script>
function getCookie(NameOfCookie)
{
    if (document.cookie.length > 0) 
{ 
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1)
   { 
    begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
       
      } 
  }
return null;  
}
</script>

<script>

    // This code returns the cookie name.
    alert("Getting HTTP Cookie");
    alert(getCookie("MyHttpCookie"));

    // Because the cookie is set to HttpOnly,
    // this returns null.
    alert("Getting HTTP Only Cookie");
    alert(getCookie("MyHttpOnlyCookie"));

</script> 


</body>
</html>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

HttpCookie.HttpOnly プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HttpCookie.HttpOnly プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS