Asp.Net 2.0的Profile

在Membership表中可以存儲一些用戶的基本信息,但有的時候,我們需要記錄的用戶信息遠遠不止Membership表中提供的這些,如QQ、MSN、家庭住址、聯繫電話等等。那如何把這些用戶信息記錄到數據庫中呢?在asp.net2.0中爲我們提供了個性設置的功能――Profile。下面看一下Profile的幾個特徵:

1) Profile根據每個用戶存儲各自的用戶資料,包括匿名稱用的資料。

2) Profile可以在Web.Config中定義而立即生效,不必手動擴充數據庫字段。

3) Profile可以存儲任意數據類型,包括簡單數據類型和自定義的複雜數據類型。

那Profile是如何實現上面這些功能呢?

Asp.net2.0中爲每一個登錄用戶驗證其身份,對匿名請求用戶生成一個GUID,這是一個唯一標識用戶身份的代號,這樣對於每一個請求的用戶都無可遁形,並且各自的身份標識都互不干擾。那asp.net如何實現在不擴充字段的基礎上,隨意地擴充用戶其它信息呢?大家打開SqlServer2005數據庫中的aspnet_profile表會看到其中有兩個字段PropertyNames和PropertyValuesString。PropertyValuesString字段中存的是你新增用戶資料的所有信息,它是以文本流的形式存儲的,而PropertyNames字段中描述如何解析PropertyValuesString字段的內容,它也是以文本流的形式存在。這樣你就可以自定義任意字段並把信息寫在表裏面。

下面看一下如何實現Profile文件的讀取和寫入:

1、擴充“真實姓名”,“年齡”和“學校”三個自定義的用戶信息

第一步:定義設置文件

<system.web>

       <profile>

<properties>

           <add name="name" type="System.String"></add>

           <add name="age" type="System.Int32"></add>

           <add name="school" type="System.String"></add>

         </properties>

       </profile>

</system.web>

第二步:在VS2005中使用Profile

將Profile寫入數據庫

if (User.Identity.IsAuthenticated)

{

Profile.name = txtName.Text;

Profile.age = Convert.ToInt32( txtAge.Text);

Profile.school = txtSchool.Text;

}

將Profile從數據庫中讀出到頁面

if (User.Identity.IsAuthenticated)

{

txtName.Text = Profile.name;

txtAge.Text = Profile.age.ToString();

txtSchool.Text = Profile.school;

}

第三步:查看aspnet_profile表,你會發現其中加入了你的自定義的信息。


2、在現有的自定義資料中加入出生日期和血型這兩個自定義資料,出生日期和血型可以作爲一個組進行設置

第一步:定義設置文件

<system.web>

       <profile>

<properties>

           <add name="name" type="System.String"></add>

           <add name="age" type="System.Int32"></add>

           <add name="school" type="System.String"></add>

           <group name="other">

             <add name="birthday" type="System.DateTime" ></add>

             <add name="blood" type="String"></add>

           </group>

         </properties>

       </profile>

</system.web>

第二步:在VS2005中使用Profile

將Profile寫入數據庫

if (User.Identity.IsAuthenticated)

{

Profile.name = txtName.Text;

Profile.age = Convert.ToInt32(txtAge.Text);

Profile.school = txtSchool.Text;

Profile.other.birthday = Convert.ToDateTime(txtBirthday.Text);

Profile.other.blood = txtBlood.Text;

}

將Profile從數據庫中讀出到頁面

if (User.Identity.IsAuthenticated)

{

txtName.Text = Profile.name;

txtAge.Text = Profile.age.ToString();

txtSchool.Text = Profile.school;

txtBirthday.Text = Profile.other.birthday.ToString();

txtBlood.Text = Profile.other.blood;

}

第三步:查看aspnet_profile表,你會發現其中加入了你的自定義的信息。

3、更新Profile用戶設置文件

第一步:設置Web.Config文件

第二步:加入更新代碼

if (User.Identity.IsAuthenticated)

{

Profile.name = txtName.Text;

Profile.age = Convert.ToInt32(txtAge.Text);

Profile.school = txtSchool.Text;

Profile.other.birthday = Convert.ToDateTime(txtBirthday.Text);

Profile.other.blood = txtBlood.Text;

Profile.Save();

}

第三步:查看aspnet_profile表,你會發現其中修改了你的自定義的信息。

上面我們用Profile.age等方式可以讀取用戶的年齡和其它的信息,但有的時候我們要查詢顯示所有用戶的信息,但asp.net沒有提供查詢所有用戶信息的功能,我們只能對現有的用戶逐一查詢其Profile信息。

查詢Profile信息

第一步:設置配置文件

第二步:得到所有的用戶

MembershipUserCollection users = Membership.GetAllUsers();

這裏用到了Membership類的GetAllUsers()方法,此內容已經在“用戶與角色管理”中說過,不再贅述。

第三步:編歷users集合,取了每一個用戶的Profile

foreach (MembershipUser singleUser in Users)

{

      ProfileCommon userprofile = Profile.GetProfile(singleUser.UserName);

      Response.Write(userprofile.name);

      Response.Write(userprofile.age);

      Response.Write(userprofile.school);

}


讀取匿名用戶的Profile

匿名用戶也有Profile?答案是肯定的。前面說過,asp.net2.0加入了一個匿名跟蹤機制,它可以產生一個獨一無二的GUID識別碼附加到未經過驗證的網頁的Request中。

默認的“匿名身份識別”是disabled,因此如果要想讓你的網站識別匿名用戶需要在Web.Config文件中進行如下配置:

<system.web>

<anonymousIdentification enabled="true"/ >

</system.web>

設置完畢就可以通過this.Request.AnonymousID取出用戶的GUID。

使用匿名用戶Profile的場境:

1) 過去做購物網站時,我們將購物車放在Session中,但Session有一定的過期策略,一般爲20分鐘。如果我們逛了半小時才進行結賬的話,那購物車的數據通過Profile保存是最好的。

2) 有人逛購物網站並不喜歡登錄後再購買,而時逛着逛着發現一個好東西,這裏再去登錄購買的話就會讓用戶感到有點煩,尤其在網絡速度比較慢的情況下。這時可以使用匿名身份對用戶購買的東西進行記錄,在結賬的時候再讓用戶輸入賬號密碼,進行結賬。

使用匿名Profile有兩個關鍵點

1)      將anonymousIdentification的enable屬性設爲true

2)      將相應匿名保存的Profile字段屬性也設爲allowAnonymous="true"

使用匿名Profile存儲信息

第一步:將anonymousIdentification的enable屬性設爲true

<anonymousIdentification enabled="true" cookieless="UseCookies"></anonymousIdentification>

第二步:在Web.Config文件中將“前景色”與“背景色”兩個屬性設爲allowAnonymous="true"

       <profile>

         <properties>

           <add name="name" type="System.String"></add>

           <add name="age" type="System.Int32"></add>

           <add name="school" type="System.String"></add>

           <group name="color">

             <add name="forecolor" type="System.Drawing.Color" serializeAs="Binary" allowAnonymous="true"></add>

             <add name="backcolor" type="System.Drawing.Color" serializeAs="Binary" allowAnonymous="true"></add>

           </group>

         </properties>

     <profile>

第三步:設置匿名用戶的“前景色”和“背景色”

Profile.color.backcolor = Color.FromName(listBack.Text);

Profile.color.forecolor = Color.FromName(listFore.Text);

Label1.ForeColor = Profile.color.forecolor;

Label1.BackColor = Profile.color.backcolor;

第四步:查看aspnet_profile表中的內容,發現顏色被存進表中了。

匿名者的Profile向登錄用戶的遷移(Migration)

第一步:如上

第二步:如上

第三步:如上

第四步:在網站的Global.asax中添加下列代碼:

     void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs args)

{

      //取得匿名用戶的ID

         ProfileCommon anonyProfile = Profile.GetProfile(args.AnonymousID);

         if ((anonyProfile.color.backcolor != null) && (anonyProfile.color.forecolor != null))

         {

             Profile.color.forecolor = anonyProfile.color.forecolor;

             Profile.color.backcolor = anonyProfile.color.backcolor;

             Profile.Save();

         }

         //刪除匿名用戶的Profile

         ProfileManager.DeleteProfile(args.AnonymousID);

         //清除匿名用戶的Cookie或身份資料

         AnonymousIdentificationModule.ClearAnonymousIdentifier();
}

用戶登錄時就會引發Profile_MigrateAnonymous事件將匿名用戶遷移到用戶的Profile

 

轉至:http://hi.baidu.com/siceblue/blog/item/04ffff8ba391aad5fd1f10fe.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章