Export (0) Print
Expand All

HttpRequest.AnonymousID Property

Gets the anonymous identifier for the user, if present.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public string AnonymousID { get; internal set; }

Property Value

Type: System.String
A string representing the current anonymous user identifier.

The AnonymousID property assigns a long-lived unique identifier to a non-authenticated user, which can be used to track the user or assign profile properties to that user without storing data in a Session object. By default, the AnonymousID property is tracked using a cookie, but it can be set to use the URI when the Cookieless attribute in the anonymous identification configuration section is set to either the UseUri, UseDeviceProfile, or AutoDetect value. You must explicitly clear the cookie if you no longer want it available, for example when an anonymous user is authenticated.

Anonymous identification is used when there is a need to identify entities that are not authenticated and when authorization is required. For more information, see anonymousIdentification Element (ASP.NET Settings Schema)

The following example shows how to use the AnonymousID property by handling the AnonymousIdentificationModule.Creating event in the Global.asax file. This example has two parts:

The first part of the code example shows how to set the AnonymousID property by handling the AnonymousIdentificationModule.Creating event in the Global.asax file. The method that is named AnonymousIdentification_Creating sets the AnonymousID property when an anonymous ID is created.

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;
}

The second part of the code example shows how to display the new AnonymousID that is created by the AnonymousIdentification_Creating event handler in the preceding example.

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

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft