Asp.net-personalization

提供:Dev Guides
移動先:案内検索

ASP.NET-パーソナライゼーション

Webサイトは、ユーザーから繰り返しアクセスするように設計されています。 パーソナライズにより、サイトはユーザーIDやその他の情報の詳細を記憶でき、各ユーザーに個人的な環境を提示します。

ASP.NETは、特定のクライアントの好みや好みに合わせてWebサイトをパーソナライズするサービスを提供します。

プロファイルについて

ASP.NETパーソナライゼーションサービスは、ユーザープロファイルに基づいています。 ユーザープロファイルは、サイトに必要なユーザーに関する情報の種類を定義します。 たとえば、名前、年齢、住所、生年月日、電話番号。

この情報は、アプリケーションのweb.configファイルで定義され、ASP.NETランタイムはそれを読み取って使用します。 このジョブは、パーソナライゼーションプロバイダーによって行われます。

ユーザーデータから取得したユーザープロファイルは、ASP.NETによって作成された既定のデータベースに格納されます。 プロファイルを保存するための独自のデータベースを作成できます。 プロファイルデータの定義は、構成ファイルweb.configに保存されます。

名前、住所、生年月日などのユーザーの詳細をアプリケーションに記憶させるサンプルサイトを作成しましょう。 <system.web>要素内のweb.configファイルにプロファイルの詳細を追加します。

<configuration>
<system.web>

<profile>
   <properties>
      <add name="Name" type ="String"/>
      <add name="Birthday" type ="System.DateTime"/>

      <group name="Address">
         <add name="Street"/>
         <add name="City"/>
         <add name="State"/>
         <add name="Zipcode"/>
      </group>

   </properties>
</profile>

</system.web>
</configuration>

プロファイルがweb.configファイルで定義されている場合、現在のHttpContextにあるプロファイルプロパティを介してプロファイルを使用でき、ページからも使用できます。

テキストボックスを追加して、プロファイルで定義されているユーザー入力を取得し、データを送信するためのボタンを追加します。

カスタマイズ

Page_loadを更新して、プロファイル情報を表示します。

using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      if (!this.IsPostBack)
      {
         ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

         if (pc != null)
         {
            this.txtname.Text = pc.Name;
            this.txtaddr.Text = pc.Address.Street;
            this.txtcity.Text = pc.Address.City;
            this.txtstate.Text = pc.Address.State;
            this.txtzip.Text = pc.Address.Zipcode;
            this.Calendar1.SelectedDate = pc.Birthday;
         }
      }
   }
}

ユーザーデータをプロファイルに保存するために、[送信]ボタンに次のハンドラーを記述します。

protected void btnsubmit_Click(object sender, EventArgs e)
{
   ProfileCommon pc=this.Profile.GetProfile(Profile.UserName);

   if (pc != null)
   {
      pc.Name = this.txtname.Text;
      pc.Address.Street = this.txtaddr.Text;
      pc.Address.City = this.txtcity.Text;
      pc.Address.State = this.txtstate.Text;
      pc.Address.Zipcode = this.txtzip.Text;
      pc.Birthday = this.Calendar1.SelectedDate;

      pc.Save();
   }
}

ページが初めて実行されるとき、ユーザーは情報を入力する必要があります。 ただし、次回はユーザーの詳細が自動的にロードされます。

<add>要素の属性

使用した名前とタイプの属性とは別に、<add>要素には他の属性があります。 次の表に、これらの属性の一部を示します。

Attributes Description
name The name of the property.
type By default the type is string but it allows any fully qualified class name as data type.
serializeAs The format to use when serializing this value.
readOnly A read only profile value cannot be changed, by default this property is false.
defaultValue A default value that is used if the profile does not exist or does not have information.
allowAnonymous A Boolean value indicating whether this property can be used with the anonymous profiles.
Provider The profiles provider that should be used to manage just this property.

匿名パーソナライゼーション

匿名のパーソナライズにより、ユーザーは自分を識別する前にサイトをパーソナライズできます。 たとえば、Amazon.comでは、ユーザーがログインする前にショッピングカートにアイテムを追加できます。 この機能を有効にするには、web.configファイルを次のように構成できます。

<anonymousIdentification enabled ="true" cookieName=".ASPXANONYMOUSUSER"
   cookieTimeout="120000" cookiePath="/" cookieRequiresSSL="false"
   cookieSlidingExpiration="true" cookieprotection="Encryption"
   coolieless="UseDeviceProfile"/>