標籤:blog http 使用 檔案 資料 cti
既然由登入控制項涉及到了Membership,那麼就不得不提到使用者個人化Profile對象。個人化允許為使用者儲存特定的個人化資訊到資料庫中,因此它不同於ASP.NET狀態管理之處在於可以永久性儲存這些資訊,很有必要強調一下web application與website的一個不同,web application中無法象website中那樣,直接用Profile對象(http://hi.baidu.com/windlhj/blog/item/8f4c4a13779de02fdc5401b7.html,web application和website相互轉化http://blog.csdn.net/guwenzhong/archive/2009/11/10/4792814.aspx)。個人化的設定與Membership的設定非常類似:
1.配置個人化提供者
預設個人化提供者是SqlProfileProvider,它使用ASP.NET.MDF(怎麼樣是不是非常熟悉,忘記的話去看Membership的設定)儲存個人化資訊。實際上你按照Membership的設定建立好資料存放區,也就把個人化的資料存放區建立好了,因為Membership與Profile用的是一個資料庫(包括利用aspnet_regsql.exe更換資料庫以後),使用者的個性資訊儲存在資料庫的aspnet_profile表中。與Membership一樣Profile的個性提供者也是在根web.config裡配置的。因為之前已經配置好了資料庫連接節點,所以這裡只配個人化提供者就行了:
<system.web>
<profile defaultProvider="MyFirstSqlProfileProvider">
<providers>
<clear/>
<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>
</providers>
</profile>
</system.web>
這樣就配置好了個人化提供者,其名字是MyFirstSqlProfileProvider。
而我們既然要儲存使用者的個人化資訊到aspnet_profile表中,我們必須為其添加屬性,而這些屬性要在<profile>節點內的<properties>裡用<add>標籤添加,並且至少要有一個屬性名稱:
<system.web>
<profile defaultProvider="MyFirstSqlProfileProvider">
<providers>
<clear/>
<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>
</providers>
<properties>
<add name="QQ" type="string"/>
<add name="Age" type="Int32"/>
<add name="Address" type="string"/>
<add name="Tel" type="string"/>
</properties>
</profile>
</system.web>
profile屬性的意思:name(屬性名稱,必須)type(屬性類型,預設是string)serializeAs(當序列化時使用的格式)
readOnly(唯讀屬性)defaultValue(屬性的預設值)allowAnonymous(布爾值,是否允許匿名使用者讀取和設定該屬性)
Provider(該屬性關聯的個人化提供者)customProviderData(允許使用者傳遞自訂資料到個人化提供者)
使用個人化屬性
在預設情況下只有驗證使用者才能讀取和寫入個人化資訊到資料庫,舉例如下:
//直接給profile賦值就可儲存使用者個人化,很類似session
protected void Button1_Click(object sender, EventArgs e)
{
//在webapp中無法像website中那樣直接使用profile對象,引入using System.Web.Profile還是無法直接用,但可以使用ProfileBase,ProfileManager,ProfileMigrateEventArgs等類。
//儲存個人化資訊
HttpContext.Current.Profile.SetPropertyValue("QQ", TextBox9.Text);
HttpContext.Current.Profile.SetPropertyValue("Age", int.Parse(DropDownList2.SelectedValue));
HttpContext.Current.Profile.SetPropertyValue("Tel", TextBox10.Text);
HttpContext.Current.Profile.SetPropertyValue("Address",TextBox11.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
//擷取個人化資訊
Label1.Text = "目前使用者:" + HttpContext.Current.Profile.UserName + "QQ:" + HttpContext.Current.Profile.GetPropertyValue("QQ") + "Age:" + HttpContext.Current.Profile.GetPropertyValue("Age") + "Tel" + HttpContext.Current.Profile.GetPropertyValue("Tel") + "Address" + HttpContext.Current.Profile.GetPropertyValue("Address");
}
還可以給個人化屬性分組便於管理:在web.config中<properties>節點內加<group>再添加組名跟屬性即可:
<system.web>
<profile defaultProvider="MyFirstSqlProfileProvider">
<providers>
<clear/>
<add name="MyFirstSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider"
connectionStringName="newsystemConnectionString" applicationName="ProfileDemo"/>
</providers>
<properties>
<add name="QQ" type="string"/>
<add name="Age" type="Int32"/>
<add name="Address" type="string"/>
<add name="Tel" type="string"/>
<group name="Habits">
<add name="likebooks"/>
<add name="likesports"/>
</group>
</properties>
</profile>
</system.web>
那些屬性的type類型也可以是自己寫的類(該類既可以在App_Code裡(App_Code相當於當前命名空間裡的子命名空間(在webapp裡使用注意其裡面類的屬性要改成編譯才能被智能提示))也可以在已編譯好的DLL裡(DLL是程式集的一種形式(另一種是exe)但在Web.config裡一定要寫全類所在的命名空間.類名,否則會出現找不到類型的錯誤):
如 <add name="ZiDingDe" type="Dingyibymyself.personlity"/> 屬性名稱ZiDingDe 類型 寫全類所在的命名空間.類名 Dingyibymyself.personlity
再如在App_Code裡的自訂類 <add name="ZiDingDe2" type="WebApplication1.App_Code.personlity"/> 是不是就是子命名空間?
http://www.cnblogs.com/xlb2000/archive/2010/08/25/1796405.html(注意作者在最後對序列化及Profile對自訂類的序列化處理的解釋)在aspnet_proile表裡PropertyNames裡是屬性名稱(包括自訂的屬性名稱)PropertyValuesString裡是簡單類型屬性的值PropertyValuesBinary裡是自訂類型屬性的值(此時類的對象已被xml序列化或二進位序列化)。由於擷取自訂類型屬性值時獲得的是一個類的對象(已經反xml或二進位序列化回來),所以要取得自訂類裡的屬性值要先建立一個引用指向它,然後再用引用取得自訂類裡的屬性值: protected void Button1_Click(object sender, EventArgs e) { person pp = (person)HttpContext.Current.Profile.GetPropertyValue("ZiDingDe"); Label1.Text = "目前使用者:" + HttpContext.Current.Profile.UserName + "QQ"+pp.QQ+"Age"+pp.Age+"Tel"+pp.Tel+"Address"+pp.Address; }匿名使用者個人化預設情況下,匿名使用者不能儲存個人化資訊,但有些時候(比如購物網匿名使用者也可以向購物車添加商品)就需要為匿名使用者建立一些臨時的個人化資訊。ASP.NET的匿名識別特性可以為匿名使用者產生一個隨機的標識符,用來唯一識別該匿名使用者,然後將匿名使用者的個人化資訊儲存到資料庫。但匿名個人化會在資料庫中儲存很多無用的資訊。開啟匿名個人化方法:在根web.config的<system.web>節點下的添加<anonymousIdentification enable="true"/>元素。然後在<properties>中指定哪些屬性可以被匿名使用者用來儲存資訊(如<add name="Book"allowAnonymous="true"/>)。然後但匿名使用者儲存這些個人化屬性資訊時就像使用者儲存那樣處理就可以了。Profile實現匿名購物車:http://www.cnblogs.com/jason-xiao/archive/2009/03/18/1415343.html試想如果僅僅是這樣那不就沒什麼實際作用還白白增加了一些無用資訊?(還是舉購物網的例子,如果匿名使用者在匿名時向購物車添加了一些商品,之後該使用者又登入了,那麼他之前添加的商品會被複製到其使用者帳號下)所以ASP.NET提供了Profile.MigrateAnonymous事件(只要匿名識別有效且目前使用者被驗證通過就會觸發該事件)可以在Global.asax(全域應用程式檔案:該檔案包含響應ASP.NET或HTTP 模組所引發的應用程式層級和會話層級事件的代碼,只在希望處理應用程式事件或會話事件時,才應建立它)中處理,這這個事件中主要處理3個步驟:1.調用Profile.GetProfile方法擷取匿名使用者的個人化資訊2.將這些個人化資訊複製到匿名使用者登入後的使用者個人化資訊裡3.移除匿名時的個人化資訊,清除匿名標記。Global.asax中要引用System.Web.Profile,MigrateAnonymous事件代碼: protected void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs e) { //webapp依舊不能用ProfileCommon類,看來這能用利用Web Profile Builder了。 //擷取匿名使用者的個人化資訊 ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID); //將匿名時使用者個人化資訊複製到使用者登入後的個人化資訊裡 Profile.使用者屬性=anonProfile.匿名時屬性 //刪除匿名時的個人化資訊和匿名識別符 ProfileManager.DeleteProfile(e.AnonymousID); AnonymousIdentificationModule.ClearAnonymousIdentifier(); //刪除匿名時產生的匿名使用者 Membership.DeleteUser(e.AnonymousID,true); } 自動儲存個人化資訊預設ASP.NET每當頁面結束生命週期時都會自動儲存個人化資訊而不管個人化資訊是否改變,這會浪費資源。所以我們要設成在只有在改變個人化資訊時才在頁面結束生命週期時儲存個人化資訊:1.將<profile>節點裡的automationSaveEnabled="false"就是不讓它自動儲存個人化資訊(也就是我們手動來調用它的儲存方法)2.手動來調用它的儲存方法:在改變了個人化資訊後顯示調用HttpContext.Current.Profile.Save()來手動控制,拿上邊的例子來說: protected void Button1_Click(object sender, EventArgs e) { HttpContext.Current.Profile.SetPropertyValue("QQ", TextBox9.Text); HttpContext.Current.Profile.SetPropertyValue("Age", int.Parse(DropDownList2.SelectedValue)); HttpContext.Current.Profile.SetPropertyValue("Tel", TextBox10.Text); HttpContext.Current.Profile.SetPropertyValue("Address",TextBox11.Text); HttpContext.Current.Profile.Save(); //只是多加這一句 }