HttpCookie.HttpOnly プロパティ
アセンブリ: System.Web (system.web.dll 内)

/** @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>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からHttpCookie.HttpOnly プロパティを検索する場合は、下記のリンクをクリックしてください。

- HttpCookie.HttpOnly プロパティのページへのリンク