zoukankan      html  css  js  c++  java
  • 定义Profile用户文件详解

    综述:Profile用户设置文件的作用是为了存储用户个人独特的数据,Profile设置文件要能够运行必须要先识别用户身份,无论是通过验证还是匿名,只要是具备唯一识别用户的能力后就可以将用户的信息存储在Profile相关的数据表中,简单的说,Profile的功能是结合Login组件的功能使用的,在Login组件是见得的记录了用户的关键的信息,主要是用户名、密码、邮箱、安全问题、问题答案五个问题及用户角色的相关问题,但是关联一个用户的其他信息,例如星座、血型等基本信息则是存储在Profile文件的。

    一、定义Profile用户文件

    若要为网站加入Profile用户设置文件只需在Web.config文件中设置即可:

    <system.web>

    <profile>

             <properties>
    <add name="country" type="System.String"defaultValue="
    中国"></add>
    //有些属性是可以固定存储的,例如上面的国家的属性就固定为“中国”

               <add name="性别"type="System.String"></add>

               <addname="星座" type="System.String"></add>

               <add name="学历"type="System.String"></add>

             </properties>

        </profile>

    </system.web>


    二、添加特定用户的Profile信息

    在经过用户的身份验证之后可以通过以下的方式添加用户的信息:

        Profile.性别 = TextBox1.Text.Trim();

        Profile.星座 =TextBox2.Text.Trim();

        Profile.学历 =TextBox3.Text.Trim();

     Profile.Save();//保存用户的相关信息

    三、读取用户的相关信息

    在数据库中,用户的Profile文件信息是通过下面的样式进行存储的:


    1)可以通过下面的方式进行用户的Profile文件信息的读取:

    if (!IsPostBack)

             {

                ProfileCommon userProfile=Profile.GetProfile(Membership.GetUser().ToString());

    //获取当前用户的相关信息,主要是获取用户名称

                Label1.Text = userProfile.性别;

                Label2.Text = userProfile.星座;

                Label3.Text = userProfile.学历;

            } 


    2)此外还可以针对Profile中所定义的字段来做群分类以方便管理,如上述的CountryCity等字段可将它们分类到Address(地址)群,以后只要通过Profile.AddressIntellisense就能够调用到上述字段,设置如下:


    调用的方式如下:

    Label5.Text = userProfile.Address.city +userProfile.Address.age.ToString();

    四、Profile用户设置文件的运作原理

    在这个运作过程牵扯到三个主要的类:

    Profile

       

    Profile类、ProfileCommom类、ProfileBase类,这三个类在读取用户的Profile文件的时候有如下的关系:

     
     

     



    五、更新Profile用户设置文件

        Profile.性别 =TextBox1.Text.Trim();

        Profile.星座 =TextBox2.Text.Trim();

        Profile.学历 =TextBox3.Text.Trim();

     Profile.Save();//此处的Save操作是更新操作,而不是保存操作

    六、读取全部用户的Profile设置文件

    1)将所有用户的名称绑定到dropdownlist之后,要在dropdownlist的选项改变后触发的事件发生需要将dropdownlistautopostback属性设置为“true”;

    2)编写dropdownlistSelectedIndexChanged事件,将选择的用户的Profile文件信息绑定到gridview的数据源:

    ProfileCommon userProfile =Profile.GetProfile(DropDownList1.SelectedItem.Text);

            if(ProfileCommon.Properties.Count >= 0)

            {

                DataTabletable = new DataTable();

               table.Columns.Add("Profile属性名称", typeof(string));

               table.Columns.Add("Profile属性值", typeof(string));

                DataRowsingleRow;

     

                singleRow =table.NewRow();

               singleRow["Profile属性名称"] = "用户名称";

               singleRow["Profile属性值"] = userProfile.UserName;

               table.Rows.Add(singleRow);

     

                singleRow =table.NewRow();

               singleRow["Profile属性名称"] = "星座";

                singleRow["Profile属性值"]= userProfile.星座;

               table.Rows.Add(singleRow);

               GridView1.DataSource = table;

            GridView1.DataBind();
    前台文件:


    后台文件:

    六、针对网站的匿名用户的Profile文件的信息

    1)对于网站的匿名用户的信息不在是隐形的,网站可以正常的记录网站的匿名用户的GUID码,也就是匿名用户的唯一的识别码,但是默认的情况是不记录的,需要在配置文件启用这个功能:

    2)记录匿名用户的基本信息,显示的用户的信息如下:

    3)存储和使用匿名用户的Profile设置文件,前提条件是a.anonymousIdentification必须设置为true(也就是启用匿名用户的Profile功能);b.Profile字段的属性也必须设置allowAnonymoustrue

     

    数据库的存储形式如下:

    七、匿名用户的Profile文件信息的迁移(Migration

    1)在全局文件global.ascx文件添加转移事件

     

    全局文件的代码如下:

    <%@ ApplicationLanguage="C#" %>

    <script runat="server">

        voidApplication_Start(object sender, EventArgs e) 

        {

            // 在应用程序启动时运行的代码

        } 

        voidApplication_End(object sender, EventArgs e) 

        {

            //  在应用程序关闭时运行的代码

        }

        voidApplication_Error(object sender, EventArgs e) 

        { 

            // 在出现未处理的错误时运行的代码

        }

        voidSession_Start(object sender, EventArgs e) 

        {

            // 在新会话启动时运行的代码

        }

        voidSession_End(object sender, EventArgs e) 

        {

            // 在会话结束时运行的代码。 

            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为

            // InProc 时,才会引发Session_End 事件。如果会话模式设置为 StateServer 

            // 或 SQLServer,则不会引发该事件。

        }

        voidProfile_MigrateAnonymous(object sender, ProfileMigrateEventArgs args)

        {

            ProfileCommonanonymousProfile = Profile.GetProfile(args.AnonymousID);

            //将匿名用户的个人爱好转移到登陆者的个人爱好

            if(!string.IsNullOrEmpty(anonymousProfile.个人爱好))

            {

                Profile.个人爱好 =anonymousProfile.个人爱好;

               Profile.Save(); 

            } 

            //删除匿名用户的Profile

           ProfileManager.DeleteProfile(args.AnonymousID);

            //清除匿名用户的Cookies或身份资料

           AnonymousIdentificationModule.ClearAnonymousIdentifier();

        }

    </script>

    这样就可以将匿名用户的某些选择添加到后来登录的用户的名下,例如当匿名用户登录网站将某些商品放入到自己的购物车,在借款时就可以将这些商品转移到自己的用户名称下。

    八、ProfileModule

    该类是用于用户Profile的创建与Profile事件的管理的,其运作原理为当ASP.NET的Profile为启用状态时,ASP.NET使用ProfileModule类创建用户Profile,并将它存储在目前的HttpContext的Profile属性中,而其管理的事件主要有三个:MigrateAonnymous(匿名者Profile迁移事件)、Personalize(Profile的个性化事件)、ProfileAutoSaving事件。

    至于各个事件的具体功能可详细查看信息。

  • 相关阅读:
    linux驱动开发学习一:创建一个字符设备
    如何高效的对有序数组去重
    找到缺失的第一个正整数
    .NET不可变集合已经正式发布
    中国人唯一不认可的成功——就是家庭的和睦,人生的平淡【转】
    自己动手搭建 MongoDB 环境,并建立一个 .NET HelloWorld 程序测试
    ASP.NET MVC 中如何用自定义 Handler 来处理来自 AJAX 请求的 HttpRequestValidationException 错误
    自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
    ServiceStack 介绍
    一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
  • 原文地址:https://www.cnblogs.com/hxwzwiy/p/2627821.html
Copyright © 2011-2022 走看看