国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET登錄控件延伸(個性化)

2019-11-14 16:36:26
字體:
來源:轉載
供稿:網友

既然由登錄控件涉及到了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://m.survivalescaperooms.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://m.survivalescaperooms.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();   //只是多加這一句
        }

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰西县| 林州市| 卢湾区| 体育| 登封市| 平江县| 沂水县| 通化县| 建德市| 武夷山市| 延长县| 温宿县| 岳普湖县| 溧水县| 筠连县| 从化市| 柳州市| 明溪县| 罗城| 华蓥市| 胶州市| 乌海市| 东平县| 双江| 江永县| 紫阳县| 上高县| 嘉义市| 延津县| 新田县| 登封市| 平潭县| 娱乐| 太仓市| 怀宁县| 瑞安市| 沙湾县| 吉水县| 清镇市| 清苑县| 闽清县|