HttpRequest.AnonymousID Proprietà

Definizione

Ottiene l'identificatore anonimo per l'utente, se presente.

public:
 property System::String ^ AnonymousID { System::String ^ get(); };
public string AnonymousID { get; }
member this.AnonymousID : string
Public ReadOnly Property AnonymousID As String

Valore della proprietà

Stringa che rappresenta l'identificatore utente anonimo corrente.

Esempio

Nell'esempio seguente viene illustrato come usare la AnonymousID proprietà gestendo l'evento Creating nel file Global.asax. In questo esempio sono presenti due parti:

  • Metodo nel file Global.asax che gestisce l'evento Creating .

  • Pagina Web Forms.

La prima parte dell'esempio di codice illustra come impostare la AnonymousID proprietà gestendo l'evento Creating nel file Global.asax. Il metodo denominato AnonymousIdentification_Creating imposta la AnonymousID proprietà quando viene creato un ID anonimo.

void Application_Start(Object sender, EventArgs e)
    {
        // Initialize user count property
        Application["UserCount"] = 0;
    }
    
public void AnonymousIdentification_Creating(Object sender, AnonymousIdentificationEventArgs e)
    {
    // Change the anonymous id
    e.AnonymousID = "mysite.com_Anonymous_User_" + DateTime.Now.Ticks;

    // Increment count of unique anonymous users
    Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    'Initialize user count property
    Application("UserCount") = 0

End Sub

Sub AnonymousIdentification_Creating(ByVal sender As Object, ByVal e As AnonymousIdentificationEventArgs)

    ' Change the anonymous id
    e.AnonymousID = "mysite.com_Anonymous_User_" & DateTime.Now.Ticks

    ' Increment count of unique anonymous users
    Application("UserCount") = Int32.Parse(Application("UserCount").ToString()) + 1

End Sub

La seconda parte dell'esempio di codice illustra come visualizzare il nuovo AnonymousID creato dal AnonymousIdentification_Creating gestore eventi nell'esempio precedente.

<%@ 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)
    {
      if (Application["UserCount"] != null)
      {
          lblUserCount.Text = Application["UserCount"].ToString();
          lblCurrentUser.Text = Request.AnonymousID;
      }
  }    
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>AnonymousID Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Number of users: 
        <asp:Label ID="lblUserCount" Runat="server"></asp:Label><br />
    Current user:
        <asp:Label ID="lblCurrentUser" Runat="server"></asp:Label><br />
    </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)

    If (Application("UserCount") IsNot Nothing) Then
      lblUserCount.Text = Application("UserCount").ToString()
      lblCurrentUser.Text = Request.AnonymousID
    End If
      
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>AnonymousID Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Number of users: 
        <asp:Label ID="lblUserCount" Runat="server"></asp:Label><br />
    Current user:
        <asp:Label ID="lblCurrentUser" Runat="server"></asp:Label><br />
    </div>
    </form>
</body>
</html>

Commenti

La AnonymousID proprietà assegna un identificatore univoco di lunga durata a un utente non autenticato, che può essere usato per tenere traccia delle proprietà dell'utente o assegnare le proprietà del profilo a tale utente senza archiviare i dati in un Session oggetto. Per impostazione predefinita, la AnonymousID proprietà viene rilevata usando un cookie, ma può essere impostata per usare l'URI quando l'attributo Cookieless nella sezione configurazione di identificazione anonima è impostato su UseUri, UseDeviceProfileo AutoDetect valore. È necessario cancellare in modo esplicito il cookie se non lo si vuole più disponibile, ad esempio quando un utente anonimo viene autenticato.

L'identificazione anonima viene usata quando è necessario identificare le entità non autenticate e quando è necessaria l'autorizzazione. Per altre informazioni, vedere elemento anonymousIdentification (ASP.NET Settings Schema).

Si applica a

Vedi anche