码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-09-13 11:56:05      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

 

 

在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)--转载来自车老师

标签:

原文地址:http://www.cnblogs.com/ooip/p/4793797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!