Pet shop 4.0中profile的介绍

    技术2022-05-11  14

    hoppingCart.aspx中 Profile 提供的功能是针对用户的个性化服务。在 ASP.NET 1.x 版本时,我们可以利用 Session Cookie 等方法来存储用户的状态信息。然而 Session 对象是具有生存期的,一旦生存期结束,该对象保留的值就会失效。 Cookie 将用户信息保存在客户端,它具有一定的安全隐患,一些重要的信息不能存储在 Cookie 中。一旦客户端禁止使用 Cookie ,则该功能就将失去应用的作用。   Profile 的出现解决了如上的烦恼,它可以将用户的个人化信息保存在指定的数据库中。 ASP.NET 2.0 Profile 功能默认支持 Access 数据库和 SQL Server 数据库,如果需要支持其他数据库,可以编写相关的 ProfileProvider 类。 Profile 对象是强类型的,我们可以为用户信息建立属性,以 PetShop 4.0 为例,它建立了 ShoppingCart WishList AccountInfo 属性。 < properties >                    < add name = "ShoppingCart"type="PetShop.BLL.Cart"allowAnonymous="true"provider="ShoppingCartProvider"/>                    < add name = "WishList"type="PetShop.BLL.Cart"allowAnonymous="true"provider="WishListProvider"/>                    < add name = "AccountInfo"type="PetShop.Model.AddressInfo"allowAnonymous="false"provider="AccountInfoProvider"/> </ properties >   在我们为 web.config 配置文件中对 Profile 进行配置后,启动 Web 应用程序, ASP.NET 会根据该配置文件中的相关配置创建一个 ProfileCommon 类的实例 ( 单步调试可以看到创建 ProfileCommon ) 。该类继承自 System.Web.Profile.ProfileBase 类。然后调用从父类继承来的 GetPropertyValue SetPropertyValue 方法,检索和设置配置文件的属性值。然后, ASP.NET 将创建好的 ProfileCommon 实例设置为页面的 Profile 属性值。因而,我们可以通过智能感知获取 Profile ShoppingCart 属性,同时也可以利用 ProfileCommon 继承自 ProfileBase 类的 Save() 方法,根据属性值更新 Profile 的数据源。     可选的 Boolean 属性。指定是否启用匿名标识。如果为 true,则使用 Cookie(或没有 Cookie 的值)来管理用户的匿名标识符。默认值为 false。 < anonymousIdentification enabled = "true"/>   在有些情况下,您的应用程序最初可能维护着匿名用户的个性化设置信息,但最后该用户登录到了您的应用程序中。在这种情况下,该用户的标识会从分配的匿名用户标识更改为身份验证进程提供的标识。 当用户登录(即不再是匿名用户)时,将引发 MigrateAnonymous 事件。如果有必要,可以对此事件进行处理,以便将信息从用户的匿名标识迁移到新的通过身份验证的标识。下面的代码示例演示用户通过身份验证时如何迁移信息。 void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e) {         ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID); foreach (CartItemInfo cartItem in anonProfile.ShoppingCart.CartItems)              Profile.ShoppingCart.Add(cartItem);           foreach (CartItemInfo cartItem in anonProfile.WishList.CartItems)             Profile.WishList.Add(cartItem); // Clean up anonymous profile         ProfileManager.DeleteProfile(e.AnonymousID);         AnonymousIdentificationModule.ClearAnonymousIdentifier();         // Save profile         Profile.Save();     }       ASP.NET 使用此方法利用在 ASP.NET 应用程序配置文件 (Web.config) 中指定的属性值初始化 SqlProfileProvider public override void Initialize(string name, NameValueCollection config) { if (config == null)          throw new ArgumentNullException("config");       if(string.IsNullOrEmpty(config["description"])) {           config.Remove("description");           config.Add("description", "Pet Shop Custom Profile Provider");               } if (string.IsNullOrEmpty(name))          name = "PetShopProfileProvider";      if(config["applicationName"] != null&& !string.IsNullOrEmpty(config["applicationName"].Trim()))                                 applicationName = config["applicationName"]; base .Initialize(name, config); }    

    最新回复(0)