ASP.NET 2.0学习之Profile

在Web.config中定义Profile

Profile结构在Web.config中使用name/type键值对定义:

<system.web>
   
<profile automaticSaveEnabled="true" >
      
<properties>
         
<add name="NumVisits" type="System.Int32"/>
         
<add name="UserName" type="System.String"/>
         
<add name="Gender" type="bool">
         
<add name="Birthday" type="System.DateTime">
      
</properties>
   
</profile>
</system.web
一但定义了profile后,就能通过HttpContext中的Profile属性来使用了(Page中也同样可以使用).

使用 Profile Information
在网站中使用Profile,和访问Session的方式是相同的.然而,作为键/值对或索引访问的替代,ASP.NET编译器会通过在Web.config中定义的结构来综合成一个名为ProfileCommon的类,它继承于ProfileBase类,这个类会将Web.config中定义的Profile插入为属性,如下:
public class ProfileCommon : ProfileBase
{
   
public  virtual  HttpProfile GetProfile(string username);
   
public  object GetPropertyValue(string propertyName);
   
public  void SetPropertyValue(string propertyName,
              
object propertyValue);
   
public  HttpProfileGroupBase GetProfileGroup(String groupName);
   
public  void Initialize(String username,Boolean isAuthenticated);
   
public  virtual void Save();
   
public  void Initialize(SettingsContext context,
             SettingsPropertyCollection properties,
             SettingsProviderCollection providers);
   
public string UserName{getset;};
   
public int NumVisits{getset;};
   
public bool Gender(getset; );
   
public DateTime Birthdate{getset; };
}
要访问profile的属性,可以通过 Page.Profile.属性 来访问,Page.Profile是ProfileCommon类的实例.


保存Profile的更改
保存更改是很简单的事情,在配置Web.config中的Profile结构时,如果automaticSaveEnabled属性设为true(默认值),则 ProfileModule 对象将在 ASP.NET 页执行结束时的 EndRequest 事件过程中引发 ProfileAutoSaving 事件,并调用 Save 方法。当然,你也可以将automaticSaveEnabled属性设为false,然后只在必要的时侯自己来调Profile.Save()


Profiles and Users
在默认情况下,profiles只对通过身份验证的用户有效,profile信息通过当前用户的身份来标识,ASP.NET在默认情况下使用当前HttpContext中User.Identity.Name作为主键来保存数据.

profile同样也支持匿名用户.匿名用户的profile是通过在Cookie中生成一个唯一值,然后和ASP.NET保存起来的profile对应的.要使ASP.NET支持匿名用户使用profile,还需要在Web.config中配置<anonymousIdentification>,如:
<configuration>
<system.web>

   
<authentication mode="Forms" />
      
<anonymousIdentification enabled="true" />
<profile>
   
<properties>
                  
<add name="NumVistors" allowAnonymous="true" type="System.Int32" defaultValue="0" />
               
</properties>
           
</profile>
</system.web>
</configuration>


配置Profile Provider
<configuration>
<connectionStrings>
<add
name="myConnectionString"
connectionString
=
"Server=MyServer;Trusted_Connection=true;database=MyDatabase"
 />
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" />
<profile defaultProvider="MyProfileProvider">
<providers>
<add
name="MyProfileProvider"
type
="System.Web.Profile.SqlProfileProvider"
connectionStringName
="myConnectionString" />
</providers>
<properties>
<add
name="FirstName"
allowAnonymous
="true" />
<add
name="LastName"
allowAnonymous
="true" />
</properties>
</profile>
</system.web>
</configuration>




Profile概述
一个使用Profile的简单例子
定义Profile
Profile更改后的保存
使用Profile组
使用自定义的Profile类型
使用匿名Profile
配置Profile Provider
管理profile

posted @ 2006-12-04 10:55  蛤蟆  阅读(619)  评论(0)    收藏  举报