petshop4.0 profile

http://www.cnblogs.com/njnudt/archive/2007/09/03/880324.html

http://www.cnblogs.com/Caceolod/articles/1190175.html

http://www.cnblogs.com/YoungPeng/archive/2008/04/30/1177629.html

http://www.cnblogs.com/howsmile/archive/2006/09/18/507524.aspx

http://blog.csdn.net/swort_177/archive/2008/01/02/2010920.aspx

 更好的答案再这里   http://tech.it168.com/KnowledgeBase/Articles/f/f/e/ffe7c061cc088327a26fc29950dc7914.htm

http://blog.csdn.net/xc_lw/archive/2006/11/20/1398425.aspx

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属性值。

----------

<system.web>
<profileautomaticSaveEnabled="false" defaultProvider="默认的Profile Provider">
<providers>
<addname="自己命名的Provider名称" connectionStringName="配置文件里的数据库链接名称" type="自己的Provider实现类,需要自己重写" applicationName="自己定义的应用程序名称"/>
</providers>
<properties>
<addname="自己命名的Profile名称" type="自己的类,需要构建" allowAnonymous="false" provider="上面的自己命名的Provider名称"/>
</properties>
</profile>
</system.web>

注意事项:
1、Profile的type可以为自己的类,包括业务实例或方法等。
2、通过重写ProfileProvider里的部份方法,实现在自己的数据库里记录自己想要的个性化用户信息。
3、如果每个用户有大量的数据信息,则该类数据信息不要通过Profile来记录。
4、被定义为Profile的type的类,必须在类定义前增加[Serializable]以实现可序列化。
5、接口定义的类型必须为类。
6、Profile Provider类里必须增加System.Web.Profile和System.Configuration的引用到项目中。
7、如果需要支持匿名用户,增加<anonymousIdentification enabled="true" />到web.config里,在profile之间。
8、使用Profile.Save方法后,将调用ProfileDALFactory里的方法将数据保存到数据库。

原文地址:https://www.cnblogs.com/barney/p/1249655.html