http://msdn2.microsoft.com/zh-cn/library/ms164644(VS.80).aspx

在Membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止Membership表中提供的这些,如QQ、MSN、家庭住址、联系电话等等。那如何把这些用户信息记录到数据库中呢?在asp.net2.0中为我们提供了个性设置的功能――Profile。下面看一下Profile的几个特征:

1) Profile根据每个用户存储各自的用户资料,包括匿名称用的资料。

2) Profile可以在Web.Config中定义而立即生效,不必手动扩充数据库字段。

3) Profile可以存储任意数据类型,包括简单数据类型和自定义的复杂数据类型。

那Profile是如何实现上面这些功能呢?

Asp.net2.0中为每一个登录用户验证其身份,对匿名请求用户生成一个GUID,这是一个唯一标识用户身份的代号,这样对于每一个请求的用户都无可遁形,并且各自的身份标识都互不干扰。那asp.net如何实现在不扩充字段的基础上,随意地扩充用户其它信息呢?大家打开SqlServer2005数据库中的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.net2.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

文章来自:http://www.2oks.cn/blog/blogview.asp?logID=300

转载于:https://www.cnblogs.com/yiki/archive/2007/08/21/863997.html

关于 Profile相关推荐

  1. /etc/profile ,/etc/bashrc ,~/.bash_profile,~/ .bashrc 区别与联系

    /etc/profile 针对系统中的每个用户,首次登录时被一次执行: /etc/bashrc 每次运行bash shell的用户都执行此文件,当bash被打开时,该文件被读取: ~/.bash_pr ...

  2. linux下使用source /etc/profile保存配置后,新的环境变量只能在一个终端里面有效

    博客园 首页 新随笔 联系 管理 订阅 <div class="blogStats"><!--done--> 随笔- 6  文章- 2  评论- 2 < ...

  3. [AutoMapper]反射自动注册AutoMapper Profile

    AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...

  4. Springboot使用Maven Profile和Spring Profile进行多环境配置

    Springboot使用Maven Profile和Spring Profile进行多环境配置 目的 在实际的项目上,一般会分三种环境dev.test.prod来方便我们的开发和部署,要求我们在开发的 ...

  5. 如何对正在运行的进程,进行heap profile

    简单来说, 就是先preload上tcmalloc, 日常用用没啥问题, 当感觉出现问题时, gdb attach 上, 然后执行 call HeapProfilerStart("xxx&q ...

  6. CoreCRM 开发实录 —— Profile

    再简单的功能,也需要一坨代码的支持.Profile 的编辑功能主要就是修改个人的信息.比如用户名.头像.性别.电话--虽然只是一个编辑界面,但添加下来,涉及了6个文件的修改和7个新创建的文件.各种生成 ...

  7. MySQL常用性能分析方法-profile,explain,索引

    1.查版本号 无论做什么都要确认版本号,不同的版本号下会有各种差异. >Select  version(); 2.执行状态分析 显示哪些线程正在运行 >show processlist; ...

  8. 【GStreamer】使用capsfilter设置x264enc中的profile级别

    1.问题描述 在[GStreamer]在x264enc中设置profile级别中,通过设置x264enc的属性,只将profile由high级别切换到main,但是在切换到baseline时,失败了. ...

  9. 【GStreamer】在x264enc中设置profile级别

    1.问题描述 在使用GStreamer生成h.264的rtmp流时,不知道怎么设置h.264的profile级别.默认一直是"high": video/x-h264, ... pr ...

  10. Android上成功实现了蓝牙的一些Profile

    前段时间做蓝牙方面的开发,Google的Android只实现了Handset/Handfree和A2DP/AVRCP等Profile,而其 它常用的Profile如HID/DUN/SPP/OPP/FT ...

最新文章

  1. Ubuntu 17.4下如何安装和配置flash player
  2. 完整java开发中JDBC连接数据库代码和步骤
  3. 修改Moodle的日期显示改英文为中文
  4. 解决移动端 手机号input 属性为 number,maxlength无效情况
  5. java向数组中插入元素
  6. 以太坊使用puppeth工具
  7. 10 种最流行的 Web 挖掘工具
  8. 鼠标显示效果的形状设置
  9. 基于JVM原理JMM模型和CPU缓存模型深入理解Java并发编程
  10. 基于飞桨实现BigGAN生成动漫图像——为艺术创作赋能
  11. [项目管理]-第十二章:项目监督和控制
  12. python使用作为转义符的开始符号_python转义符的使用
  13. mysql 14 关闭休眠链接
  14. 搞事课堂 C++格式化硬盘
  15. 华清远见Qt作业网络聊天室1014
  16. element单独选择年或年月或年月日以及起止日期使用el-date-picker
  17. 微信公众平台的运营管理
  18. pom 备注_POMGH-25POMGH-25
  19. php 密码错误就弹出框_PHP弹出对话框
  20. 基于STM32F103C8T6实现流水灯的操作实验

热门文章

  1. XP蓝屏代码集(转)
  2. td中文字间距_怎么做?文字编排创意的小心思
  3. 数据结构——>稀疏数组
  4. php 获取扩展函数,获取php扩展函数
  5. mysql 事物状态有几种_关于MySQL的二十个经典面试题
  6. python app逆向_python之app逆向破解data参数中的PassWord DES加密无填充
  7. android连接airprint打印机,AirPrint:iOS的打印机
  8. linux进程间通信练习:对于text.txt文件,子进程将字符串“something communication”写入text.txt,父进程读取text.txt文件内容并打印。
  9. python自学月收入20k_每天自学2小时,18周便可月入20K,437集python自学资料拿走不谢...
  10. 12. Django基础:模型层及ORM