ASP.NET使用者個人化Profile——匿名使用者向註冊使用者移轉

來源:互聯網
上載者:User

  在前面的例子當中,使用者如果要使用Profile,可以有兩種狀態,一種是登陸後使用,另一種是不登陸匿名使用Profile。但是在一些電子商務應用中,往往使用者在匿名狀態中使用購物車,但是在結賬的時候是要求登陸或是註冊的。這就需要將使用者匿名狀態下添加進購物車的資料遷入使用者登入後的購物車當中。要實現這種效果就必須編寫代碼才能實現:

 

  在實現本例的過程中,關鍵是實現了ProfileModule類中的MigrateAnonymous事件。該事件在匿名使用者登入時,且Profile中存在資料時觸發。與其他事件不同的是,該事件處理常式必須在Global.asax檔案中定義。

 

具體做法如下:

在項目中添加一個Global.asax檔案,複製以下代碼到檔案當中

 

    void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs pe)
    {
        //取得該使用者在匿名狀態下的資料
        ProfileCommon pc = Profile.GetProfile(pe.AnonymousID);
        //判斷購物車中是否添加了資料
        if (pc.Cart.Count!=0)
        {
            //如果有資料,則將匿名狀態下添加的商品添加到登陸後的購物車當中
            //注意;Profile.Cart存放的是登陸後的資料
            //pc.Cart是該使用者在匿名狀態下添加的資料
            Profile.Cart = pc.Cart;
        }
        //刪除匿名使用者在aspnet_users表中的記錄
        Membership.DeleteUser(pe.AnonymousID);
        //刪除匿名使用者Profile資料
        ProfileManager.DeleteProfile(pe.AnonymousID);
        //刪除匿名使用者標識
        AnonymousIdentificationModule.ClearAnonymousIdentifier();
   }

 

 ok.就這麼簡單~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.