现在的位置: 首页 > 综合 > 正文

ASP.net2.0 Mebership之补充:Profile

2013年07月15日 ⁄ 综合 ⁄ 共 1902字 ⁄ 字号 评论关闭
 

去年毕业设计时,写过两篇Blog

1,  关于membership实用性讨论

2,  关于membership的进一步理解

虽然是“进一步理解”,但实话说,当时我仍是一知半解。那时,我的理解: Membership只能提供帐号,密码,Email,安全问题及解答这五个核心部分,用户的一些其他信息如:性别,年龄,学历……等等是不能提供的。在这工作一年里,我独立从事的小项目里我采用“Membership+自己设计的表”来解决这一问题的。

但是,直到今天我才在一本书中找到了合适的解决方案,那就是使用Profile——当时我也注意过Proflie,但可能当时时间紧,事情也多,没求甚解,结果把这个重要的东西给忽略了。

 

Profile的特征

1,  Profile依个别用户帐号而存储个别用户资料

2,  Profile数据字段可以自行在Web.config中定义而立即生效,不用在数据库中扩充字段

3,  Profile对于数据的读取属于强类型,能够存储不仅限于如stringint 等简单数据类型,可以是如XMLBinary等复杂数据类型。

4,  可以设计用来存储用户个性化的信息

 

配置web.config

加入我还需要用户的性别,年龄,住址信息。

web.config  system.web配置节中这样配置

<profile>

      <properties>

        <add name="Sex" type="System.Boolean"/>

        <add name="Age" type="System.Int32"/>

        <group name="Address">

          <add name="Country" type="System.String" defaultValue="China"/>

          <add name ="City" type="System.String"/>

        </group>

      </properties>

</profile>

从以上代码可以看出,可以设置默认值,还可以对一些信息进行分组

 

配置完web.config以后 其实vs2005自动在数据库中 aspnet_ProfilePropertyNames (字段名)、PropertyValuesString (字段值)字段添加了内容,字段之间用“:”来隔开,并且记录了起始点和字段长度。因此也可以看出Profile不适合无限制的添加字段,即使不限制,但会影响数据库性能。

在程序中这样使用:

Profile.Address.Country = "China";

Profile.Address.City = "Beijing";

Profile.Sex = true;

Profile.Age = 25;

Profile.Save();

 

这样使用的前提是用户登录以后,这样保存的是当前登录用户的Profile

 

若要查看所有用户的Profile需要这样:

foreach (MembershipUser user in Membership.GetAllUsers())

        {

            ProfileCommon userProfile = Profile.get(user.UserName);

            if(ProfileCommon.Properties.Count>=0)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("Sex");

                dt.Columns.Add("Age");

                dt.Columns.Add("Country");

                dt.Columns.Add("City");

 

                DataRow dr = dt.NewRow();

                dr["Sex"]=userProfile.Sex;

                dr["Age"] = userProfile.Age;

                dr["Country"] = userProfile.Address.Country;

                dr["City"] = userProfile.Address.City;

 

                dt.Rows.Add(dr);

            }

        }

 

这样就把所有用户Profile存到了一个DataTable

抱歉!评论已关闭.