asp.net2.0学习----个性化用户配置

个性化用户配置

一、简介

为用户提供自定义的外观、内容、布局,当用户再次访问的时候,用户还能看到自己原来的设定。

 

二、个性化的三大步骤

1.  识别用户身份

要建立验证用户身份的机制

创建识别用户需求的机制

创建管理用户的机制

2.  提供个性化服务

针对注册和匿名用户提供不同的服务

3.  存贮用户信息

可以保存用户的相关信息,以方便下次使用,包括用户的登陆信息

三、实现个性化服务的三大功能

1.  个性化用户配置

2.  WEB部件

3.  成员和角色管理

四、为匿名用户进行个性化设置

web.config配置

    <anonymousIdentification enabled="true"/>

    <profile>

      <properties>

        <add name="Name" allowAnonymous="true" />

        <add name="LastSubmit" type="System.DateTime" allowAnonymous="true"/>

        <group name="Address">

          <add name="City"  allowAnonymous="true"/>

          <add name="PostalCode"  allowAnonymous="true"/>

        </group>

      </properties>

</profile>

代码:

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            //显示用户配置信息

            DisplayProfileInfo();

        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        //保存用户配置信息到Profile属性中

        Profile.Name = txtName.Text;

        Profile.Address.City = txtCity.Text;

        Profile.Address.PostalCode = txtPostalCode.Text;

        Profile.LastSubmit = DateTime.Now;

        //显示用户配置信息

        DisplayProfileInfo();

    }

    private void DisplayProfileInfo()

    {

        //Profile属性中获取数据并赋值给服务器控件           

        txtName.Text = Profile.Name;

        txtCity.Text = Profile.Address.City;

        txtPostalCode.Text = Profile.Address.PostalCode;

        DateTime time = Profile.LastSubmit;

        //如果未获取值则显示空,否则显示获取的值

        if (time.Year == 1)

        {

            labLastSubmit.Text = "";

        }

        else

        {

            labLastSubmit.Text = time.ToString();

        }

    }

五、为注册用户实现个性化用户配置

web.config配置

    <connectionStrings>

        <add name="NorthwindConnectionString" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"

            providerName="System.Data.SqlClient" />

    </connectionStrings>

    <system.web>        

          <profile>

              <properties>

                   <add name="ShoppingCart" type="ShoppingCart" serializeAs="Binary"/>

              </properties>           

          </profile>

          <authorization>

              <deny users="?"/>

          </authorization>

          <authentication mode="Forms">

              <forms loginUrl ="Login.aspx"></forms>

          </authentication>

代码示例:code 13-2

六、匿名用户转化为注册用户的处理

Global.asax中的设置

    void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)

    {

        //获取匿名用户的Profile对象

        ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);

        //如果总价为不为0(说明匿名用户进行了选择),则将匿名用户的Profile存储起来

        if (anonProfile.ShoppingCart.Total  != 0)

        {           

            Profile.ShoppingCart = anonProfile.ShoppingCart;           

        }       

        //删除匿名用户的用户数据(从aspnet_Users表)

        Membership.DeleteUser(pe.AnonymousID);

        //删除匿名用户的Profle数据(从aspnet_Profile表)

        ProfileManager.DeleteProfile(pe.AnonymousID);

        //删除匿名用户标识

        AnonymousIdentificationModule.ClearAnonymousIdentifier();       

}

       示例代码:code 13-3

七、删除个性化信息

删除匿名用户的个性化信息

ProfileManager.DeleteProfile(Context.Request.AnonymousID)

删除注册用户的个性化信息

ProfileManager.DeleteProfile(User.Identity.Name) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值