HttpCookie Classe

Definizione

Specifica una modalità indipendente dai tipi per creare e modificare singoli cookie HTTP.

public ref class HttpCookie sealed
public sealed class HttpCookie
type HttpCookie = class
Public NotInheritable Class HttpCookie
Ereditarietà
HttpCookie

Esempio

Nell'esempio di codice seguente viene illustrato come verificare la presenza di un cookie denominato DateCookieExample nell'oggetto HttpRequest . Se il cookie non viene trovato, viene creato e aggiunto all'oggetto HttpResponse . Il cookie è impostato per scadere in 10 minuti.

<%@ 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">

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        // Get cookie from the current request.
        HttpCookie cookie = Request.Cookies.Get("DateCookieExample");
        
        // Check if cookie exists in the current request.
        if (cookie == null)
        {
            sb.Append("Cookie was not received from the client. ");
            sb.Append("Creating cookie to add to the response. <br/>");
            // Create cookie.
            cookie = new HttpCookie("DateCookieExample");
            // Set value of cookie to current date time.
            cookie.Value = DateTime.Now.ToString();
            // Set cookie to expire in 10 minutes.
            cookie.Expires = DateTime.Now.AddMinutes(10d);
            // Insert the cookie in the current HttpResponse.
            Response.Cookies.Add(cookie);
        }
        else
        {
            sb.Append("Cookie retrieved from client. <br/>");
            sb.Append("Cookie Name: " + cookie.Name + "<br/>");
            sb.Append("Cookie Value: " + cookie.Value + "<br/>");
            sb.Append("Cookie Expiration Date: " + 
                cookie.Expires.ToString() + "<br/>");
        }
        Label1.Text = sb.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpCookie Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Label1" runat="server"></asp:Label>
    </div>
    </form>
</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)
        Dim sb As New StringBuilder()
        ' Get cookie from current request.
        Dim cookie As HttpCookie
        cookie = Request.Cookies.Get("DateCookieExample")
        
        ' Check if cookie exists in the current request
        If (cookie Is Nothing) Then
            sb.Append("Cookie was not received from the client. ")
            sb.Append("Creating cookie to add to the response. <br/>")
            ' Create cookie.
            cookie = New HttpCookie("DateCookieExample")
            ' Set value of cookie to current date time.
            cookie.Value = DateTime.Now.ToString()
            ' Set cookie to expire in 10 minutes.
            cookie.Expires = DateTime.Now.AddMinutes(10D)
            ' Insert the cookie in the current HttpResponse.
            Response.Cookies.Add(cookie)
        Else
            sb.Append("Cookie retrieved from client. <br/>")
            sb.Append("Cookie Name: " + cookie.Name + "<br/>")
            sb.Append("Cookie Value: " + cookie.Value + "<br/>")
            sb.Append("Cookie Expiration Date: " & _
                cookie.Expires.ToString() & "<br/>")
        End If
        Label1.Text = sb.ToString()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpCookie Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Label1" runat="server"></asp:Label>    
    </div>
    </form>
</body>
</html>

Commenti

La HttpCookie classe ottiene e imposta le proprietà dei singoli cookie. La HttpCookieCollection classe fornisce metodi per archiviare, recuperare e gestire più cookie.

ASP.NET include due raccolte intrinseche di cookie. La raccolta a cui si accede tramite la Cookies raccolta dell'oggetto HttpRequest contiene cookie trasmessi dal client al server nell'intestazione Cookie . La raccolta a cui si accede tramite la Cookies raccolta dell'oggetto HttpResponse contiene nuovi cookie creati nel server e trasmessi al client nell'intestazione della Set-Cookie risposta HTTP.

Costruttori

HttpCookie(String)

Crea e assegna un nome a un nuovo cookie.

HttpCookie(String, String)

Crea e assegna un nome e un valore a un nuovo cookie.

Proprietà

Domain

Ottiene o imposta il dominio a cui associare il cookie.

Expires

Ottiene o imposta la data e l'ora di scadenza per il cookie.

HasKeys

Ottiene un valore che indica se il cookie ha sottochiavi.

HttpOnly

Ottiene o imposta un valore che specifica se un cookie è accessibile da parte di script del lato client.

Item[String]

Ottiene un collegamento alla proprietà Values. Questa proprietà viene offerta per supportare la compatibilità con le versioni precedenti di ASP (Active Server Pages).

Name

Ottiene o imposta il nome di un cookie.

Path

Ottiene o imposta il percorso virtuale da trasmettere con il cookie corrente.

SameSite

Ottiene o imposta il valore dell'attributo SameSite del cookie.

Secure

Ottiene o imposta un valore che indica se trasmettere il cookie mediante SSL (Secure Sockets Layer), ossia solo su HTTPS.

Shareable

Determina se al cookie è consentito partecipare alla memorizzazione nella cache dell'output.

Value

Ottiene o imposta un singolo valore di cookie.

Values

Ottiene una raccolta di coppie chiave/valore contenute in un singolo oggetto cookie.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)
TryParse(String, HttpCookie)

Converte la rappresentazione di stringa specificata di un cookie nell'equivalente HttpCookie e restituisce un valore che indica se la conversione è stata eseguita correttamente.

Si applica a

Vedi anche