上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其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

(车延禄)

收藏于 2007-07-03

asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师的更多相关文章

  1. asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师

    在Membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止Membership表中提供的这些,如QQ.MSN.家庭住址.联系电话等等.那如何把这些用户信息记录到数 ...

  2. asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师

    Membership.MembershipUser和Roles类 用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的. Membership:用户成员账号管理, ...

  3. asp.net2.0安全性(1)--用户角色篇(起篇)--转载来自车老师

    安全管理的解决方案在.net1.1中几乎为一片空白,对于应用程序的验证与授权大部分的工作是开发人员自己编写代码,或者是借助企业库等工具来实现,此可谓.net1.1中的一大缺憾.在.net2.0中微软为 ...

  4. asp.net2.0安全性(3)--验证与授权--转载来自车老师

    "验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...

  5. asp.net2.0导出pdf文件完美解决方案【转载】

    asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...

  6. asp.net2.0安全性(4)--Login系列控件--转载来自车老师

    前面主要说了与安全相关的一系列的类,现在我们使用这些类就可以做出我们自己的安全系统了.其实微软的目的远不至于此,下面我们就来看一下微软为我们提供的Login系列控件. Login系列控件是微软为了简化 ...

  7. asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师

    创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...

  8. asp.net2.0安全性(1)--用户角色篇(代码实现2)--转载来自车老师

    加载所有用户 MembershipUserCollection user = Membership.GetAllUsers(); listUser.DataSource = user; listUse ...

  9. Asp.Net2.0下C#环境 Login控件实现用户登录

    原文:Asp.Net2.0下C#环境 Login控件实现用户登录 一.前台显示效果 二.前台代码             <asp:Login ID="Login1" run ...

随机推荐

  1. C# 计算器 运算符和数字键的keys对照

    keys. private void Computer_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad0) ...

  2. Java 输出通过 InetAddress 获得的 IP 地址数组

    使用 InetAddress 获取 IP 地址会得到一个 byte 数组 如果你直接输出这个数组,你会发现 IP 地址中的某些位变成了负数 比如 61.135.169.105 会输出成 61.-121 ...

  3. reason: 'unable to dequeue a cell with identifier Cell

    今天在cell重用的时候出现一下错误 reason:  'unable  to  dequeue  a  cell  with  identifier  Cell  -  must  register ...

  4. [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

    题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...

  5. memcache 在php存取中的应用

    当用户刷新网页或有大量用户访问网站时,就会产生大量数据库查询进程,这不但拖慢了网页打开速度,同时也给服务器带来了很大压力. 作为php菜鸟,今天刚刚接触到了 memcache 这个东东,于是自己跟着文 ...

  6. 版本管理神器git上手

    由于以前折腾过svn,虽然最终没有用成功,但是也算有经验,git入门还是比较简单的. 在新目录下建立初始化版本库  : git init git add file git add file2 git ...

  7. OpenBlas编译方法(体验msys下使用MingW)

    OpenBlas是一个优化的Blas库,基于GotoBlas21.13 BSD版,安装步骤如下: Windows下安装: 1. 在SourgeForge下载最新的OpenBlas库:http://so ...

  8. java代码中获取进程process id(转)

    另一方面,线程ID=进程ID+内部线程对象ID并不成立,    参考: blog.csdn.net/heyetina/article/details/6633901     如何在java代码中获取进 ...

  9. Jekyll搭建过程详解

    原先博客用Jekyll搭建在Github上,近来访问缓慢,而且markdown不太方便写,故决定在博客园安个新家. 文章见Github博客: 搭建过程:http://wuxichen.github.i ...

  10. Sereja and Bottles

    http://codeforces.com/problemset/problem/315/A 题目意思是第ai的瓶子能开bi的瓶子.给你相应的数据,求无法用其他瓶子打开的数量(即需要外力). 一开始看 ...