HttpRequest.AnonymousID HttpRequest.AnonymousID HttpRequest.AnonymousID HttpRequest.AnonymousID Property

定义

获取该用户的匿名标识符(如果存在)。Gets the anonymous identifier for the user, if present.

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

属性值

字符串,表示当前匿名用户标识符。A string representing the current anonymous user identifier.

示例

下面的示例演示如何通过处理 global.asax AnonymousID文件中的Creating事件来使用属性。The following example shows how to use the AnonymousID property by handling the Creating event in the Global.asax file. 此示例包含两个部分:This example has two parts:

  • Global.asax 文件中处理Creating事件的方法。A method in the Global.asax file that handles the Creating event.

  • Web 窗体页。A Web Forms page.

此代码示例的第一部分演示如何通过在 global.asax 文件AnonymousIDCreating处理事件来设置属性。The first part of the code example shows how to set the AnonymousID property by handling the Creating event in the Global.asax file. 命名AnonymousIdentification_Creating的方法在创建匿名 ID AnonymousID时设置属性。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;
}
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

此代码示例的第二部分演示如何显示在前面的AnonymousID示例中AnonymousIdentification_Creating由事件处理程序创建的新。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>
<%@ 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>

注解

属性为未经过身份验证的用户分配长生存期的唯一标识符,该标识符可用于跟踪用户或将配置文件属性分配给该用户,而无需将数据Session存储在对象中。 AnonymousIDThe 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. 默认情况下, AnonymousID使用 cookie 跟踪属性,但在 "匿名标识配置" 部分的Cookieless属性设置为UseUriUseDeviceProfileAutoDetect时,可以将其设置为使用 URI负值.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. 如果不再需要 cookie (例如,对匿名用户进行身份验证),则必须显式清除该 cookie。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. 有关详细信息,请参阅AnonymousIdentification 元素(ASP.NET 设置架构)For more information, see anonymousIdentification Element (ASP.NET Settings Schema)

适用于

另请参阅