Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
HttpRequest Class
 AnonymousID Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
HttpRequest..::.AnonymousID Property

Gets the anonymous identifier for the user, if present.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public ReadOnly Property AnonymousID As String
Visual Basic (Usage)
Dim instance As HttpRequest
Dim value As String

value = instance.AnonymousID
C#
public string AnonymousID { get; }
Visual C++
public:
property String^ AnonymousID {
    String^ get ();
}
JScript
public function get AnonymousID () : String

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 code example demonstrates how to use the AnonymousId()()() property by accessing the AnonymousIdentification_OnCreate event in the Global.asax file. This example has two parts:

  • A Web Forms page.

  • A method that handles the AnonymousIdentification_OnCreate event.

The first part of the code example shows how to use the AnonymousId()()() property by accessing the AnonymousIdentification_OnCreate event in the Global.asax file. The AnonymousIdentification_OnCreate event is raised whenever a new anonymous ID is created.

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

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

The second part of the code example shows how to display the new AnonymousId()()() created by the AnonymousIdentification_OnCreate event in the preceding example.

Visual Basic
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

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

End Sub

Sub AnonymousIdentification_OnCreate(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


C#
void Application_Start(Object sender, EventArgs e)
    {
        // Initialize user count property
        Application["UserCount"] = 0;
    }

public void AnonymousIdentification_OnCreate(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;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Name of event to handle is incorrect      reidca ... Thomas Lee   |   Edit   |   Show History
Note the code above is incorrect.

The actual name of the event in Global.asax is AnonymousIdentification_Creating

e.g.

public void AnonymousIdentification_Creating(Object sender, System.Web.Security.AnonymousIdentificationEventArgs e)
{
// Change the anonymous id
e.AnonymousID = "mysite.com_Anonymous_User_" + DateTime.Now.Ticks;
}

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker