HttpRequest.AnonymousID Propiedad

Definición

Obtiene el identificador anónimo del usuario, si lo hay.

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

Valor de propiedad

Cadena que representa el identificador anónimo de usuario actual.

Ejemplos

En el ejemplo siguiente se muestra cómo usar la AnonymousID propiedad controlando el Creating evento en el archivo Global.asax. Este ejemplo tiene dos partes:

  • Método en el archivo Global.asax que controla el Creating evento.

  • Página de Web Forms.

La primera parte del ejemplo de código muestra cómo establecer la AnonymousID propiedad controlando el Creating evento en el archivo Global.asax. El método con nombre AnonymousIdentification_Creating establece la AnonymousID propiedad cuando se crea un identificador anónimo.

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 segunda parte del ejemplo de código muestra cómo mostrar el nuevo AnonymousID creado por el AnonymousIdentification_Creating controlador de eventos en el ejemplo anterior.

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

Comentarios

La AnonymousID propiedad asigna un identificador único de larga duración a un usuario no autenticado, que se puede usar para realizar un seguimiento del usuario o asignar propiedades de perfil a ese usuario sin almacenar datos en un Session objeto. De forma predeterminada, se realiza un seguimiento de la AnonymousID propiedad mediante una cookie, pero se puede establecer para usar el URI cuando el Cookieless atributo de la sección de configuración de identificación anónima se establece en el UseUrivalor , UseDeviceProfileo AutoDetect . Debe borrar explícitamente la cookie si ya no quiere que esté disponible, por ejemplo, cuando se autentica un usuario anónimo.

La identificación anónima se usa cuando es necesario identificar entidades que no están autenticadas y cuando se requiere autorización. Para obtener más información, vea anonymousIdentification Element (ASP.NET Settings Schema)).

Se aplica a

Consulte también