HttpOnly (Propiedad)
HttpCookie.HttpOnly (Propiedad)
Obtiene o establece un valor que especifica si se puede obtener acceso a una cookie mediante la secuencia de comandos de cliente.

Espacio de nombres: System.Web
Ensamblado: System.Web (en system.web.dll)

Visual Basic (Declaración)
Public Property HttpOnly As Boolean
Visual Basic (Uso)
Dim instance As HttpCookie
Dim value As Boolean

value = instance.HttpOnly

instance.HttpOnly = value
C#
public bool HttpOnly { get; set; }
C++
public:
property bool HttpOnly {
    bool get ();
    void set (bool value);
}
J#
/** @property */
public boolean get_HttpOnly ()

/** @property */
public void set_HttpOnly (boolean value)
JScript
public function get HttpOnly () : boolean

public function set HttpOnly (value : boolean)
XAML
No aplicable.

Valor de propiedad

Es true si la cookie tiene el atributo HttpOnly y no se puede obtener acceso a la misma a través de una secuencia de comandos de cliente; de lo contrario, es false. El valor predeterminado es false.

El Service Pack 1 y las versiones posteriores de Microsoft Internet Explorer 6 admiten la propiedad HttpOnly para las cookies, que puede ayudar a mitigar las amenazas a las secuencias de comandos entre sitios que originan cookies robadas. Las cookies robadas pueden contener información confidencial que identifique al usuario en el sitio, como el id. de sesión de ASP.NET o el vale de autenticación mediante formularios, que el atacante puede reproducir para hacerse pasar por el usuario u obtener información confidencial. Cuando un explorador compatible recibe una cookie HttpOnly, ésta resulta inaccesible para la secuencia de comandos de cliente.

Nota de precauciónPrecaución:

Establecer la propiedad HttpOnly en true no evita que un atacante con acceso al canal de la red obtenga acceso a la cookie directamente. Como medida de protección, considere el uso de Secure Sockets Layer (SSL). También es importante la seguridad en las estaciones de trabajo, dado que un usuario malintencionado puede utilizar una ventana del explorador abierta o un equipo que contenga cookies persistentes para obtener acceso a un sitio Web con la identidad de un usuario legítimo.

Para obtener más información sobre los posibles ataques y cómo puede ayudar esta propiedad a mitigarlos, vea Mitigating Cross-site Scripting With HTTP-only Cookies (Cómo mitigar las secuencias de comandos entre sitios con cookies sólo HTTP).

En el ejemplo de código siguiente se muestra la forma de escribir una cookie HttpOnly y cómo el cliente no puede obtener acceso a ella a través de ECMAScript.

Visual Basic
<%@ 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)
    
    ' 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  >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<script type="text/javascript">
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 type="text/javascript">

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

C#
<%@ 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">
    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  >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
<script type="text/javascript">
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 type="text/javascript">

    // 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 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

.NET Framework

Compatible con: 3.0, 2.0
Page view tracker