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 @   蛤蟆  阅读(618)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示