asp.net 2.0 的 profile機制 在membership表中可以儲存一些使用者的基本資料,但有的時候,我們需要記錄的使用者資訊遠遠不止membership表中提供的這些,如qq、msn、家庭住址、聯絡電話等等。那如何把這些使用者資訊記錄到資料庫中呢?在asp.net 2.0 中為我們提供了個性設定的功能――profile。看一下profile的幾個特徵:1) profile 根據每個使用者儲存各自的使用者資料,包括匿名稱用的資料。2) profile 可以在web.config中定義而立即生效,不必手動擴充資料庫欄位。3) profile 可以儲存任意資料類型,包括單一資料型別和自訂的複雜資料類型。那 profile 是如何?上面這些功能呢?asp.net 2.0 中為每一個登入使用者驗證其身份,對匿名請求使用者產生一個guid,這是一個唯一標識使用者身份的代號,這樣對於每一個請求的使用者都無可遁形,並且各自的身份標識都互不干擾。那asp.net如何?在不擴充欄位的基礎上,隨意地擴充使用者其它資訊呢?開啟sql server 2005 資料庫中的 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集合,取了每一個使用者的profileforeach (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.net 2.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屬性設為true2) 將相應匿名儲存的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