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.

範例

下列範例示範如何使用AnonymousID屬性來處理CreatingGlobal.asax 檔案中的事件。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 Form 網頁。A Web Forms page.

在程式碼範例的第一個部分示範如何設定AnonymousID屬性來處理CreatingGlobal.asax 檔案中的事件。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設定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>

備註

AnonymousID屬性會將長時間執行的唯一識別碼指派給未驗證的使用者,這可用來追蹤使用者或指派給該使用者的設定檔屬性,而不需要資料儲存於Session物件。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. 根據預設,AnonymousID屬性會追蹤使用 cookie,但它可以設定為使用 URI 時Cookieless匿名識別的 [設定] 區段中的屬性會設為UseUriUseDeviceProfile,或AutoDetect值。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。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)

適用於

另請參閱