HttpRequest.AnonymousID 屬性

定義

取得使用者的匿名識別項 (如果有)。

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

屬性值

表示目前匿名使用者識別項的字串。

範例

下列範例示範如何在 Global.asax 檔案中處理 Creating 事件,以使用 AnonymousID 屬性。 此範例有兩個部分:

  • Global.asax 檔案中處理 Creating 事件的方法。

  • Web Form 頁面。

程式代碼範例的第一個部分示範如何在 Global.asax 檔案中處理 Creating 事件來設定 AnonymousID 屬性。 建立匿名標識碼時所命名 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 進行追蹤,但當匿名識別組態區段中的 屬性設定UseUri為 、 UseDeviceProfileAutoDetect 值時Cookieless,可以設定為使用 URI。 如果您不想再使用 Cookie,則必須明確清除 Cookie,例如匿名使用者經過驗證時。

當需要識別未驗證的實體,以及需要授權時,就會使用匿名識別。 如需詳細資訊,請參閱 anonymousIdentification 元素 (ASP.NET 設定架構)

適用於

另請參閱