asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师
在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表,你会发现其中加入了你的自定义的信息。
默认个性化设置信息被存放在app_date文件夹下的数据库中,如果相把个性化设置信息存放到指定数据库中,需要进行下列提供程序设置:
<profile>
<providers>
<remove name="AspNetSqlProfileProvider"/>
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="conn" type="System.Web.Profile.SqlProfileProvider" description="存储Profile数据"/>
<!--<add name="TextFileProfileProvider" type="CustomProviders.TextFileProfileProvider, CustomProviders" description="Text file profile provider"/>-->
</providers>
<properties>
......
</<properties>
</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表,你会发现其中修改了你的自定义的信息。
(车延禄)
收藏于 2007-06-29
asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师的更多相关文章
- asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师
上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其Profi ...
- asp.net2.0安全性(1)--用户角色篇(类)--转载来自车老师
Membership.MembershipUser和Roles类 用户与角色管理在asp.net2.0中是通过Membership和Roles两个类来实现的. Membership:用户成员账号管理, ...
- asp.net2.0安全性(1)--用户角色篇(起篇)--转载来自车老师
安全管理的解决方案在.net1.1中几乎为一片空白,对于应用程序的验证与授权大部分的工作是开发人员自己编写代码,或者是借助企业库等工具来实现,此可谓.net1.1中的一大缺憾.在.net2.0中微软为 ...
- asp.net2.0安全性(3)--验证与授权--转载来自车老师
"验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...
- asp.net2.0导出pdf文件完美解决方案【转载】
asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...
- asp.net2.0安全性(4)--Login系列控件--转载来自车老师
前面主要说了与安全相关的一系列的类,现在我们使用这些类就可以做出我们自己的安全系统了.其实微软的目的远不至于此,下面我们就来看一下微软为我们提供的Login系列控件. Login系列控件是微软为了简化 ...
- asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师
创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...
- asp.net2.0安全性(1)--用户角色篇(代码实现2)--转载来自车老师
加载所有用户 MembershipUserCollection user = Membership.GetAllUsers(); listUser.DataSource = user; listUse ...
- Asp.Net2.0下C#环境 Login控件实现用户登录
原文:Asp.Net2.0下C#环境 Login控件实现用户登录 一.前台显示效果 二.前台代码 <asp:Login ID="Login1" run ...
随机推荐
- HDU4648+Easy
N^2都能过!!!!!!! /* Easy */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...
- android代码实现关机
1.API没有开放,需要提升为syetem app级别! 2.android 模块编译,mm 命令 2.1.先进入顶层 source build/envsetup.sh 2.2.进入目录 m ...
- [Swust OJ 179]--火柴棍(找规律)
题目链接:http://acm.swust.edu.cn/problem/0179/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 541]--排列字典序问题
题目链接:http://acm.swust.edu.cn/problem/0541/ Time limit(ms): 2000 Memory limit(kb): 65535 n个元素{1,2,... ...
- centos 安装lua
yum install readline-develwget http://www.lua.org/ftp/lua-5.1.4.tar.gztar -xzvf lua-5.1.4.tar.gz3.编译 ...
- ZOJ 2852 Deck of Cards DP
题意: 一一个21点游戏. 1. 有三个牌堆,分别为1X,2X,3X. 2. 纸牌A的值为1,纸牌2-9的值与牌面面相同,10(T).J.Q.K的值为10,而而joke(F)的值为 任意大大. 3. ...
- 432B - Football Kit
解题思路: 暴力绝对TLE 一个队伍穿主场球衣的次数 = 这个队伍的客场球衣颜色与其他队主场球衣颜色起冲突的次数 + (n - 1) #include <stdio.h> #include ...
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...
- jquery删除动态增加的li
<script type="text/jscript"> //楼主帮你修改调整了下 $(document).ready(function () { $('.zuo li ...
- 浅析Java中的反射机制原理
反射反射,程序员的快乐! Java中反射机制使用的还是比较广泛的,系统的灵活性.可扩展性大都都是通过反射等方式来加载外部插件,使得系统与插件解耦的同时,增加了功能.但是很多人都只是会用,却是不知道它的 ...