HttpCookie.HttpOnly Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Recebe ou define um valor que especifica se um cookie é acessível por script do lado do cliente.
public:
property bool HttpOnly { bool get(); void set(bool value); };
public bool HttpOnly { get; set; }
member this.HttpOnly : bool with get, set
Public Property HttpOnly As Boolean
Valor de Propriedade
true se o cookie tiver o HttpOnly atributo e não puder ser acedido através de um script do lado do cliente; caso contrário, false. A predefinição é false.
Exemplos
O seguinte exemplo de código demonstra como escrever um HttpOnly cookie e mostra como este não é acessível pelo cliente através do ECMAScript.
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
<%@ 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 xmlns="http://www.w3.org/1999/xhtml" >
<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>
Observações
Microsoft Internet Explorer versão 6 Service Pack 1 e posteriores suportam uma propriedade de cookies, HttpOnly, que pode ajudar a mitigar ameaças de scripting cross-site que resultem em cookies roubados. Cookies roubados podem conter informações sensíveis que identificam o utilizador no site, como o ID de sessão ASP.NET ou um ticket de autenticação de formulários, e podem ser reproduzidos pelo atacante para se fazer passar pelo utilizador ou obter informações sensíveis. Quando um HttpOnly cookie é recebido por um navegador compatível, fica inacessível ao script do lado do cliente.
Caution
Definir a HttpOnly propriedade como true não impede que um atacante com acesso ao canal de rede aceda diretamente ao cookie. Considere usar a Camada Secure Sockets (SSL) para ajudar a proteger contra isto. A segurança da estação de trabalho também é importante, pois um utilizador malicioso pode usar uma janela aberta do navegador ou um computador contendo cookies persistentes para obter acesso a um site com a identidade legítima de um utilizador.