petshop4中profile的配置

来源:互联网 发布:图像分析算法 编辑:程序博客网 时间:2024/05/19 17:25

ProfileAuthenticationOption 枚举 描述要搜索的用户配置文件的身份验证类型。
All 搜索所有配置文件。 
Anonymous 仅搜索匿名配置文件。 
Authenticated 仅搜索已验证身份的配置文件。

web.config中profile的配置
<add name="SQLProfileConnString" connectionString="server=.;database=MSPetShop4Profile;user       id=mspetshop;password=pass@word1;min pool size=4;max pool size=4;" providerName="System.Data.SqlClient"/>
这个用来设置profile要用到的数据库

<profile automaticSaveEnabled ="false" defaultProvider ="ShoppingCartProvider">
      <providers>
        <add name ="ShoppingCartProvider" type="Zjw.Profile.PetShopProfileProvider" connectionStringName="SQLProfileConnString" applicationName=".NET Pet Shop 4.0"/>这里的name就是上面的defaultProvider的也就是要配置文件提供程序的名称,在下面也将要用到。
        <add name ="WishListProvider" type="Zjw.Profile.PetShopProfileProvider" connectionStringName="SQLProfileConnString" applicationName=".NET Pet Shop 4.0"/>
        <add name ="AccountInfoProvider" type="Zjw.Profile.PetShopProfileProvider" connectionStringName="SQLProfileConnString" applicationName=".NET Pet Shop 4.0"/>
      </providers>
      <properties>
        <add name ="ShoppingCart" type ="Zjw.BLL.Cart" allowAnonymous ="true" provider ="ShoppingCartProvider"/>
        <add name="WishList" type="Zjw.BLL.Cart" allowAnonymous="true" provider ="WishListProvider"/>
        <add name="AccountInfo" type ="Zjw.Model.AddressInfo" allowAnonymous ="true" provider ="AccountInfoProvider"/>
      </properties>
    </profile>

automaticSaveEnabled
指定用户配置文件是否在 ASP.NET 页执行结束时自动保存。如果为 true,则用户配置文件在 ASP.NET 页执行结束时自动保存。
defaultProvider
指定默认配置文件提供程序的名称。
 <providers>  </providers>可选的元素。定义配置文件提供程序的集合。
type  指定实现 ProfileProvider 抽象基类的类型。就是自己写的prifileProvider类
connectionStringName 用来连接数据库,前面的<connnection>中定义的
applicationName 应用程序的名字
<properties>      </properties>这个必须有 
其中name是要设置的profile属性,该值用作自动生成的配置文件类的属性的名称,并用作该属性在 Properties 集合中的索引值。该属性的名称不能包含句点 (.)。
type指定属性类型。
allowAnnymous 指定在应用程序用户是匿名用户的情况下是否可以获取或设置属性。
provider 就是 <providers>  </providers>中提供的名字

在匿名用户登陆后,应用程序维护的匿名用户的个性化设置信息,要变为存在数据库中的用户个性化设置。
这个时候将引发MigrateAnonymous事件。这个事件处理处理方法在global.asax中 为void MigrateAnonymous(object sender,profileMiGrateEventArgs e)中  Global.asax 文件(也叫做 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 ASP.NET 或 HTTP 模块引发的应用程序级别事件的代码。在这里主要处理一些应用程序的事件处理方法。如session_start,application_end等。
{
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
系统根据我们对profile的配置创建的profileCommon类
foreach (CartItemInfo cartItem in anonProfile.ShoppingCart.CartItems)
Profile.ShoppingCart.Add(cartItem);
foreach (CartItemInfo cartItem in anonProfile.WishList.CartItems)
Profile.WishList.Add(cartItem);
//上面是把匿名用户的
ProfileManager.DeleteProfile(e.AnonymousID);
//ProfileManager 类用于管理配置文件设置、搜索用户配置文件,以及删除不再使用的用户配置文件。是个静态类
AnonymousIdentificationModule.ClearAnonymousIdentifier();
//AnonymousIdentificationModule 类创建并管理 ASP.NET 应用程序的匿名标识符
//清除与某个会话关联的匿名 Cookie 或标识符。
//ClearAnonymousIdentifier()清除与某个会话关联的匿名 Cookie 或标识符。
//ClearAnonymousIdentifier 方法移除与网站上的会话关联的匿名标识符。当用户切换到与某个用户 ID 关联且经过身份验证的会话时(例如//,当 MigrateAnonymous 事件发生时),ClearAnonymousIdentifier 方法用于移除与某个会话关联的匿名标识符。
Profile.Save();
}
ProfileMigrateEventArgs 对象将事件信息提供给 ProfileModule 类的 MigrateAnonymous 事件。ProfileMigrateEventArgs 对象为 AnonymousID 属性中的匿名配置文件提供对当前请求的 HttpContext 和匿名用户标识符的访问。
AnonymousID 属性包含匿名用户的唯一标识符。当匿名使用应用程序的人登录时,可以处理 MigrateAnonymous 事件,以将配置文件属性值从用户的匿名配置文件复制到他(或她)已验证身份的配置文件中。
在我们为web.config配置文件中对Profile进行配置后,启动Web应用程序,ASP.NET会根据该配置文件中的相关配置创建一个ProfileCommon类的实例(单步调试可以看到创建ProfileCommon)。该类继承自System.Web.Profile.ProfileBase类。然后调用从父类继承来的GetPropertyValue和SetPropertyValue方法,检索和设置配置文件的属性值。然后,ASP.NET将创建好的ProfileCommon实例设置为页面的Profile属性值。

原创粉丝点击