HttpCookie.HttpOnly Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger ett värde som anger om en cookie är tillgänglig för skript på klientsidan.
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
Egenskapsvärde
true om cookien har HttpOnly attributet och inte kan nås via ett skript på klientsidan, falseannars . Standardvärdet är false.
Exempel
Följande kodexempel visar hur du skriver en HttpOnly cookie och visar hur den inte är tillgänglig för klienten via 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>
Kommentarer
Microsoft Internet Explorer version 6 Service Pack 1 och senare har stöd för en cookieegenskap, HttpOnly, som kan bidra till att minimera hot mot skript mellan webbplatser som resulterar i stulna cookies. Stulna cookies kan innehålla känslig information som identifierar användaren till webbplatsen, till exempel ASP.NET sessions-ID eller formulärautentiseringsbiljett, och kan spelas upp av angriparen för att maskera sig som användare eller hämta känslig information. När en HttpOnly cookie tas emot av en kompatibel webbläsare är den inte tillgänglig för skript på klientsidan.
Caution
Att ställa in egenskapen HttpOnly på true hindrar inte en angripare med åtkomst till nätverkskanalen från att komma åt cookien direkt. Överväg att använda SSL (Secure Sockets Layer) för att skydda mot detta. Säkerhet för arbetsstationer är också viktigt eftersom en obehörig användare kan använda ett öppet webbläsarfönster eller en dator som innehåller beständiga cookies för att få åtkomst till en webbplats med en legitim användares identitet.