HttpRequest.AnonymousID Propriété

Définition

Obtient l'identificateur anonyme de l'utilisateur, s'il existe.

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

Valeur de propriété

Chaîne représentant l'identificateur de l'utilisateur anonyme actuel.

Exemples

L’exemple suivant montre comment utiliser la AnonymousID propriété en gérant l’événement Creating dans le fichier Global.asax. Cet exemple comporte deux parties :

  • Méthode dans le fichier Global.asax qui gère l’événement Creating .

  • Page Web Forms.

La première partie de l’exemple de code montre comment définir la AnonymousID propriété en gérant l’événement Creating dans le fichier Global.asax. La méthode nommée AnonymousIdentification_Creating définit la propriété lors de la AnonymousID création d’un ID anonyme.

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 deuxième partie de l’exemple de code montre comment afficher le nouveau AnonymousID créé par le AnonymousIdentification_Creating gestionnaire d’événements dans l’exemple précédent.

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

Remarques

La AnonymousID propriété affecte un identificateur unique à longue durée de vie à un utilisateur non authentifié, qui peut être utilisé pour suivre l’utilisateur ou affecter des propriétés de profil à cet utilisateur sans stocker de données dans un Session objet. Par défaut, la propriété est suivie à l’aide AnonymousID d’un cookie, mais elle peut être définie pour utiliser l’URI lorsque l’attribut dans la Cookieless section configuration de l’identification anonyme est défini sur la UseUrivaleur , UseDeviceProfileou AutoDetect . Vous devez effacer explicitement le cookie si vous ne souhaitez plus qu’il soit disponible, par exemple lorsqu’un utilisateur anonyme est authentifié.

L’identification anonyme est utilisée lorsqu’il est nécessaire d’identifier les entités qui ne sont pas authentifiées et lorsque l’autorisation est requise. Pour plus d’informations, consultez anonymousIdentification, élément (schéma des paramètres ASP.NET).

S’applique à

Voir aussi