重新过一遍ASP.NET 2.0(C#)(7) - Profile(存储用户配置测试)

本文介绍ASP.NET 2.0中如何使用存储用户配置功能来定义和存储基于用户的设置,包括配置文件的设置、匿名配置文件的支持、自定义类的应用,以及如何迁移匿名配置文件到登录用户的配置文件。
介绍
ASP.NET 2.0 中的存储用户配置功能使您可以定义并存储要在整个应用程序中使用的基于用户的设置。而且,在用户未登录时,可以将这些设置存储在匿名配置文件中,然后在将来某个时间将其迁移到登录用户的配置文件中。


关键
1、配置<system.web>元素下的<profile>元素;如果需要支持匿名的话则还需要配置<system.web>元素下的<anonymousIdentification>元素。示例如下,仅为说明
None.gif      < profile  enabled ="true"  defaultProvider ="SqlProfileProvider"  inherits ="CustomProfile" >
None.gif      
< providers >
None.gif        
< add  name ="SqlProfileProvider"  type ="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
None.gif             connectionStringName
="SqlConnectionString"
None.gif             applicationName
="/"   />
None.gif      
</ providers >
None.gif      
< properties >
None.gif        
< add  name ="Name"   />
None.gif        
< add  name ="Color"  type ="System.Drawing.Color"   />
None.gif        
< group  name ="Group" >
None.gif          
< add  name ="Collection"  type ="System.Collections.ArrayList"   />
None.gif          
< add  name ="Price"  type ="int"  defaultValue ="100"   />
None.gif        
</ group >
None.gif      
</ properties >
None.gif    
</ profile >
None.gif
None.gif    
< anonymousIdentification
None.gif      
enabled ="true"
None.gif      cookieName
=".VS2005_ANONYMOUS"
None.gif      cookieTimeout
="1440"
None.gif      cookiePath
="/"
None.gif      cookieRequireSSL
="false"
None.gif      cookieSlidingExpiration
="true"
None.gif      cookieProtection
="All"
None.gif      cookieless
="UseCookies"   />
None.gif

各属性详细说明参看MSDN,索引处查找“profile 元素”和“anonymousIdentification 元素”

注意:
<profile>元素的inherits属性指定自定义类,该类要继承自ProfileBase

Profile是自动保存的,但是某些复杂类型可能无法自动保存,此时需要设置<profile>元素的automaticSaveEnabled设置为false,要保存的话则调用 Profile 上的 Save 方法即可。要动态取消Profile的自动保存功能的话则需要在 global.asax 中加一个Profile_ProfileAutoSaving事件,示例如下,仅为说明
None.gif      void  Profile_ProfileAutoSaving(Object sender, ProfileAutoSaveEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if ((e.Context.Items["CancelProfileAutoSave"!= null&& ((bool)e.Context.Items["CancelProfileAutoSave"== true))
InBlock.gif            e.ContinueWithProfileAutoSave 
= false;
ExpandedBlockEnd.gif    }

在需要取消Profile的自动保存功能的页的代码处如下写
None.gif protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  Context.Items[
"CancelProfileAutoSave"= true;    
ExpandedBlockEnd.gif}


2、通过ProfileManager执行相关任务,如搜索有关所有配置文件、经过身份验证用户的配置文件及匿名用户的配置文件的统计信息,确定在给定时间段内尚未修改的配置文件的数量,根据配置文件的上一次修改日期删除单个配置文件及多个配置文件等

3、将匿名配置文件迁移到经过身份验证的配置文件
在global.asax加一个Profile_MigrateAnonymous事件处理,示例如下,仅为说明
None.gif      void  Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif      
// 获得匿名配置
InBlock.gif
      ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);
InBlock.gif
InBlock.gif      
// 从匿名配置中取值并赋值给经过身份验证的配置
InBlock.gif
      if (anonProfile.Color != System.Drawing.Color.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        Profile.Color 
= anonProfile.Color;
ExpandedSubBlockEnd.gif      }

InBlock.gif        
InBlock.gif      
// 从数据库中删除匿名配置
InBlock.gif
      ProfileManager.DeleteProfile(pe.AnonymousID);
InBlock.gif        
InBlock.gif      
// 清除与某个会话关联的匿名 Cookie 或标识符
InBlock.gif
      AnonymousIdentificationModule.ClearAnonymousIdentifier();  
ExpandedBlockEnd.gif    }


示例
App_Code/CustomProfile.cs
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  System.Web.Profile;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif
/// CustomProfile 的摘要说明
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  CustomProfile : ProfileBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string _customName;
InBlock.gif
InBlock.gif    
public string CustomName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _customName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _customName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private bool _customSex;
InBlock.gif
InBlock.gif    
public bool CustomSex
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _customSex; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _customSex = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

web.config
None.gif      < profile  enabled ="true"  defaultProvider ="SqlProfileProvider"  inherits ="CustomProfile" >
None.gif      
< providers >
None.gif        
< add  name ="SqlProfileProvider"  type ="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
None.gif             connectionStringName
="SqlConnectionString"
None.gif             applicationName
="/"   />
None.gif      
</ providers >
None.gif      
< properties >
None.gif        
< add  name ="Name"   />
None.gif        
< add  name ="Color"  type ="System.Drawing.Color"   />
None.gif        
< group  name ="Group" >
None.gif          
< add  name ="Collection"  type ="System.Collections.ArrayList"   />
None.gif          
< add  name ="Price"  type ="int"  defaultValue ="100"   />
None.gif        
</ group >
None.gif      
</ properties >
None.gif    
</ profile >

Profile/Test.aspx
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs"
ExpandedBlockEnd.gif    Inherits
="Profile_Test" Title="存储用户配置测试" 
%>
None.gif
None.gif
< asp:Content  ID ="Content1"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
None.gif    
< asp:Label  ID ="lbl"  runat ="Server"   />
None.gif
</ asp:Content >
None.gif

Profile/Test.aspx.cs
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  Profile_Test : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 一看就懂
InBlock.gif
        Profile.Name = User.Identity.Name;
InBlock.gif        Profile.Color 
= System.Drawing.Color.AliceBlue;
InBlock.gif        Profile.Group.Collection.Clear();
InBlock.gif        Profile.Group.Collection.Add(
"冰棍");
InBlock.gif        Profile.Group.Collection.Add(
"瓜子");
InBlock.gif        Profile.Group.Price 
= 999999;
InBlock.gif
InBlock.gif        Profile.CustomName 
= User.Identity.Name;
InBlock.gif        Profile.CustomSex 
= true;
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        lbl.Text 
= "Name:" + Profile.Name + "<br />";
InBlock.gif        lbl.Text 
+= "Color:" + Profile.Color.ToString() + "<br />";
InBlock.gif        
foreach (string s in Profile.Group.Collection)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lbl.Text 
+= "商品有:" + s + "<br />";
ExpandedSubBlockEnd.gif        }

InBlock.gif        lbl.Text 
+= "价格:" + Profile.Group.Price + "<br />";
InBlock.gif
InBlock.gif        lbl.Text 
+= "自定义类名字:" + Profile.CustomName + "<br />";
InBlock.gif        lbl.Text 
+= "自定义类姓名:" + Profile.CustomSex;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

用“abc”这个用户登录后的运行结果
Name:abc
Color:Color [AliceBlue]
商品有:冰棍
商品有:瓜子
价格:999999
自定义类名字:abc
自定义类姓名:True


注:需要用aspnet_regsql配置数据库


OK
[源码下载]

转载于:https://www.cnblogs.com/hulu/archive/2007/06/27/797873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值