asp.net 2.0 的 profile机制

asp.net 2.0 的 profile机制

 
在membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止membership表中提供的这些,如qq、msn、家庭住址、联系电话等等。那如何把这些用户信息记录到数据库中呢?
在asp.net 2.0 中为我们提供了个性设置的功能――profile。
看一下profile的几个特征:
1) profile 根据每个用户存储各自的用户资料,包括匿名称用的资料。
2) profile 可以在web.config中定义而立即生效,不必手动扩充数据库字段。
3) profile 可以存储任意数据类型,包括简单数据类型和自定义的复杂数据类型。
那 profile 是如何实现上面这些功能呢?
asp.net 2.0  中为每一个登录用户验证其身份,对匿名请求用户生成一个guid,这是一个唯一标识用户身份的代号,这样对于每一个请求的用户都无可遁形,并且各自的身份标识都互不干扰。那asp.net如何实现在不扩充字段的基础上,随意地扩充用户其它信息呢?
打开sql server 2005 数据库中的 aspnet_profile 表会看到其中有两个字段propertynames和propertyvaluesstring。propertyvaluesstring字段中存的是你新增用户资料的所有信息,它是以文本流的形式存储的,而propertynames字段中描述如何解析propertyvaluesstring字段的内容,它也是以文本流的形式存在。这样你就可以自定义任意字段并把信息写在表里面。
下面看一下如何实现profile文件的读取和写入:
1、扩充“真实姓名”,“年龄”和“学校”三个自定义的用户信息
第一步:定义设置文件
<system.web>
<profile>
  <properties>
    <add name="name" type="system.string"></add>
    <add name="age" type="system.int32"></add>
    <add name="school" type="system.string"></add>
  </properties>
</profile>
</system.web>
第二步:在vs2005中使用profile
将profile写入数据库
if (user.identity.isauthenticated)
{
profile.name = txtname.text;
profile.age = convert.toint32( txtage.text);
profile.school = txtschool.text;
}
将profile从数据库中读出到页面
if (user.identity.isauthenticated)
{
txtname.text = profile.name;
txtage.text = profile.age.tostring();
txtschool.text = profile.school;
}
第三步:查看aspnet_profile表,你会发现其中加入了你的自定义的信息。
2、在现有的自定义资料中加入出生日期和血型这两个自定义资料,出生日期和血型可以作为一个组进行设置
第一步:定义设置文件
<system.web>
<profile>
  <properties>
    <add name="name" type="system.string"></add>
    <add name="age" type="system.int32"></add>
    <add name="school" type="system.string"></add>
    <group name="other">
<add name="birthday" type="system.datetime" ></add>
<add name="blood" type="string"></add>
    </group>
  </properties>
</profile>
</system.web>
第二步:在vs2005中使用profile
将profile写入数据库
if (user.identity.isauthenticated)
{
profile.name = txtname.text;
profile.age = convert.toint32(txtage.text);
profile.school = txtschool.text;
profile.other.birthday = convert.todatetime(txtbirthday.text);
profile.other.blood = txtblood.text;
}
将profile从数据库中读出到页面
if (user.identity.isauthenticated)
{
txtname.text = profile.name;
txtage.text = profile.age.tostring();
txtschool.text = profile.school;
txtbirthday.text = profile.other.birthday.tostring();
txtblood.text = profile.other.blood;
}
第三步:查看aspnet_profile表,你会发现其中加入了你的自定义的信息。
3、更新profile用户设置文件
第一步:设置web.config文件
第二步:加入更新代码
if (user.identity.isauthenticated)
{
profile.name = txtname.text;
profile.age = convert.toint32(txtage.text);
profile.school = txtschool.text;
profile.other.birthday = convert.todatetime(txtbirthday.text);
profile.other.blood = txtblood.text;
profile.save();
}
第三步:查看aspnet_profile表,你会发现其中修改了你的自定义的信息。
上面我们用profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其 profile 信息。
查询profile信息
第一步:设置配置文件
第二步:得到所有的用户
membershipusercollection users = membership.getallusers();
这里用到了membership类的getallusers()方法。
第三步:遍历users集合,取了每一个用户的profile
foreach (membershipuser singleuser in users) 
{
     profilecommon userprofile = profile.getprofile(singleuser.username);
     response.write(userprofile.name);
     response.write(userprofile.age);
     response.write(userprofile.school);
}
读取匿名用户的profile
匿名用户也有profile?答案是肯定的。前面说过,asp.net 2.0 加入了一个匿名跟踪机制,它可以产生一个独一无二的guid识别码附加到未经过验证的网页的request中。
默认的“匿名身份识别”是disabled,因此如果要想让你的网站识别匿名用户需要在web.config文件中进行如下配置:
<system.web>
<anonymousidentification enabled="true"/ >
</system.web>
设置完毕就可以通过this.request.anonymousid取出用户的guid。
使用匿名用户profile的场境:
1)过去做购物网站时,我们将购物车放在session中,但session有一定的过期策略,一般为20分钟。如果我们逛了半小时才进行结账的话,那购物车的数据通过profile保存是最好的。
2)有人逛购物网站并不喜欢登录后再购买,而时逛着逛着发现一个好东西,这里再去登录购买的话就会让用户感到有点烦,尤其在网络速度比较慢的情况下。这时可以使用匿名身份对用户购买的东西进行记录,在结账的时候再让用户输入账号密码,进行结账。
使用匿名profile有两个关键点:
1)     将anonymousidentification的enable属性设为true
2)     将相应匿名保存的profile字段属性也设为allowanonymous="true"
使用匿名profile存储信息:
第一步:将anonymousidentification的enable属性设为true
<anonymousidentification enabled="true" cookieless="usecookies"></anonymousidentification>
第二步:在web.config文件中将“前景色”与“背景色”两个属性设为allowanonymous="true"
<profile>
  <properties>
    <add name="name" type="system.string"></add>
    <add name="age" type="system.int32"></add>
    <add name="school" type="system.string"></add>
    <group name="color">
<add name="forecolor" type="system.drawing.color" serializeas="binary" allowanonymous="true"></add>
<add name="backcolor" type="system.drawing.color" serializeas="binary" allowanonymous="true"></add>
    </group>
  </properties>
    <profile>
第三步:设置匿名用户的“前景色”和“背景色”
profile.color.backcolor = color.fromname(listback.text);
profile.color.forecolor = color.fromname(listfore.text);
label1.forecolor = profile.color.forecolor;
label1.backcolor = profile.color.backcolor;
第四步:查看aspnet_profile表中的内容,发现颜色被存进表中了。
匿名者的profile向登录用户的迁移(migration)
第一步:如上
第二步:如上
第三步:如上
第四步:在网站的global.asax中添加下列代码:
    void profile_migrateanonymous(object sender, profilemigrateeventargs args)
{
     //取得匿名用户的id
  profilecommon anonyprofile = profile.getprofile(args.anonymousid);
  if ((anonyprofile.color.backcolor != null) && (anonyprofile.color.forecolor != null))
  {
profile.color.forecolor = anonyprofile.color.forecolor;
profile.color.backcolor = anonyprofile.color.backcolor;
profile.save();
  }
  //删除匿名用户的profile
  profilemanager.deleteprofile(args.anonymousid);
  //清除匿名用户的cookie或身份资料
  anonymousidentificationmodule.clearanonymousidentifier(); 
}
用户登录时就会引发profile_migrateanonymous事件将匿名用户迁移到用户的profile 

posted on 2010-06-07 21:09  henzy  阅读(391)  评论(0编辑  收藏  举报

导航