Partager via


HttpRequest.AnonymousID Propriété

Définition

Obtient l’identificateur anonyme de l’utilisateur, le cas échéant.

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 d’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 propriété en gérant l’événement AnonymousIDCreating dans le fichier Global.asax. La méthode nommée AnonymousIdentification_Creating définit la AnonymousID propriété lorsqu’un ID anonyme est créé.

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 de longue durée à 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 AnonymousID propriété est suivie à l’aide d’un cookie, mais elle peut être définie pour utiliser l’URI lorsque l’attribut Cookieless de la section de configuration d’identification anonyme est défini sur la UseUrivaleur , UseDeviceProfileou AutoDetect la valeur. Vous devez effacer explicitement le cookie si vous ne le souhaitez plus, 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 Element (ASP.NET Settings Schema).

S’applique à

Voir aussi