HttpRequest.AnonymousID プロパティ

定義

存在する場合は、ユーザーの匿名 ID を取得します。

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

プロパティ値

現在の匿名ユーザーの ID を表す文字列。

次の例では、Global.asax ファイルの イベントをCreating処理して プロパティを使用AnonymousIDする方法を示します。 この例には、次の 2 つの部分があります。

  • イベントを処理する Global.asax ファイル内の Creating メソッド。

  • Web Forms ページ。

コード例の最初の部分では、Global.asax ファイルの イベントを AnonymousID 処理して プロパティを Creating 設定する方法を示します。 という名前 AnonymousIdentification_Creating のメソッドは、匿名 ID の作成時に 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

コード例の 2 番目の部分は、前の例のイベント ハンドラーによって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 を使用して追跡されますが、匿名 ID 構成セクションの属性が 、UseDeviceProfile、または AutoDetect 値のいずれかにUseUri設定されている場合Cookielessに URI を使用するように設定できます。 匿名ユーザーが認証された場合など、Cookie を使用できないようにするには、Cookie を明示的にクリアする必要があります。

匿名 ID は、認証されていないエンティティを識別する必要がある場合と、承認が必要な場合に使用されます。 詳細については、「 anonymousIdentification 要素 (ASP.NET 設定スキーマ)」を参照してください。

適用対象

こちらもご覧ください