HttpRequest.AnonymousID 属性

定义

获取该用户的匿名标识符(如果存在)。

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

属性值

字符串,表示当前匿名用户标识符。

示例

以下示例演示如何通过处理 Creating Global.asax 文件中的 事件来使用 AnonymousID 属性。 此示例包含两个部分:

  • Global.asax 文件中处理事件 Creating 的方法。

  • Web Forms页。

代码示例的第一部分演示如何通过处理 Creating Global.asax 文件中的 事件来设置 AnonymousID 属性。 创建匿名 ID 时,名为 AnonymousIdentification_Creating 的方法将设置 AnonymousID 属性。

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

代码示例的第二部分演示如何显示由前面的示例中的AnonymousIdentification_Creating事件处理程序创建的新 AnonymousID

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

注解

属性 AnonymousID 将长期唯一标识符分配给未经身份验证的用户,该标识符可用于跟踪用户或将配置文件属性分配给该用户,而无需将数据存储在 对象中 Session 。 默认情况下,AnonymousID使用 Cookie 跟踪 属性,但当匿名标识配置部分中的 属性设置为 、 UseDeviceProfileAutoDetect 值时Cookieless,可以将其设置为UseUri使用 URI。 如果不再希望 Cookie 可用,则必须显式清除该 Cookie,例如,当匿名用户进行身份验证时。

如果需要标识未经身份验证的实体,并且需要授权,则使用匿名标识。 有关详细信息,请参阅 anonymousIdentification Element (ASP.NET Settings Schema)

适用于

另请参阅