今天在研究Asp.net 2.0的用户配置文件功能。ASP.NET 2.0 中的用户配置文件功能使您可以定义并存储要在整个应用程序中使用的基于用户的设置。而且,在用户未登录时,可以将这些设置存储在匿名配置文件中,然后在将来某个时间将其迁移到登录用户的配置文件中。
网站通常需要一个便捷方法来存储适用于站点范围的特定于用户的数据。配置文件功能为定义特定于用户的数据以及存储和检索这些用户数据提供了一种简单易用的方法。
用户配置文件是一个属性集合,这些属性定义了要为网站用户存储的信息。在一定范围内可以替代先前版本的Session变量,并且功能还要强大。用户配置文件使用简单 XML 语法定义在配置文件(machine.config 和/或 web.config)中。在页中,通过 Profile 属性引用用户配置文件信息。
在页生命周期开始之前,ASP.NET 确保 Profile 可供页使用。同样,在 ASP.NET 页生命周期结束时,ASP.NET 自动将 Profile 保存到基础数据存储区中。与成员资格和角色管理器等其他功能一样,配置文件功能在设计上采用了基于提供程序的模型。提供程序从由某个功能公开的类和业务逻辑为某个功能提取物理数据存储。配置文件功能附带了用于 Microsoft™ SQL Server 的提供程序。可以创建您自己的自定义提供程序,并将其配置为作用于任一配置文件功能。使用配置文件功能的页在使用自定义提供程序时将会照常运行。
除了 Profile 属性之外,配置文件功能还通过 ProfileManager 支持配置文件的管理(适用于经过身份验证的用户和匿名用户)。通过 ProfileManager 执行的常见任务包括:
- 搜索有关所有配置文件、经过身份验证用户的配置文件及匿名用户的配置文件的统计信息
- 确定在给定时间段内尚未修改的配置文件的数量
- 根据配置文件的上一次修改日期删除单个配置文件及多个配置文件
以上对Asp.net 2.0用户配置文件功能的介绍大多引用于Asp.net 2.0快速入门教程:http://localhost/QuickStartv20/aspnet/doc/profile/default.aspx
从以上对Asp.net 2.0用户配置文件功能的介绍可知,Asp.net 2.0用户配置文件功能必须依赖于某个提供程序(Provider)和对应的物理数据存储(可以简易理解为“数据库”)。所以,要对指定的应用程序使用Profile支持,则必须先创建提供程序(Provider)和配置数据库。由于初学,并不知道如何创建自定义提供程序,所以下面介绍的只是使用SQL Server数据库和配置文件功能附带的用于 Microsoft™ SQL Server 的提供程序。以下介绍简单的使用方法:
一、配置指定数据库以支持用户配置文件功能。这一步可参考aspnet_regsql工具的实用命令。
二、配置Web.config文件的Profile节以支持Profile功能。如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
如上代码所示,首先必须配置Profile节的enabled属性为true以支持Profile功能,另外,指定默认Provider,即defaultProvider属性。
其次,配置providers节。必须至少添加一个Provider。添加Provider时,其中name(Provider名称)、connectionStringName(连接字符串名称)、type(Provider的类型)这三个属性是必不可少的。
最后,配置Profile的相关属性<properties>元素。可以使用<add>元素添加属性,<remove>元素移除属性,<group>添加一组属性,<clear>清除所有属性。在使用<add>元素添加属性时,其中name属性是必须指定的;另外,provider属性可以指定使用的提供程序,这在<providers>元素中有多个provider时常用;可以通过type属性来指定所添加的Profile的属性的数据类型;allowAnonymous属性指定是否允许匿名用户存储数据,若此属性设为true则表示其允许匿名用户存储数据,但这要求在Web.config里设置<anonymousIdentification enabled="true"/>节以允许匿名标识。
到此,基本的<profile>元素就配置完成,其所添加的profile属性就可以在应用程序中以常规的属性访问方式来访问了,如:Profile.UserName。
大家应该注意到了在这里的Profile节里还设置了inherits属性,这个属性指定了用户自定义基类,这个用户自定义基类存储在应用程序的App_Code文件夹中,可以在这个类上面定义更多的Profile属性。可以参考以下代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
三、在应用程序中获取或设置Profile属性。请参考以下代码:
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class UpdateProfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtUserName.Text = Server.HtmlEncode(Profile.UserName);
foreach (string s in Profile.MyProjects)
{
lbProjects.Items.FindByValue(s).Selected = true;
}
cbCanOperateAllBills.Checked = Profile.CanOperateAllBills;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
Profile.CanOperateAllBills = cbCanOperateAllBills.Checked;
Profile.UserName = txtUserName.Text;
Profile.MyProjects.Clear();
for (int i = 0; i < lbProjects.Items.Count; i++)
{
if (lbProjects.Items[i].Selected)
{
Profile.MyProjects.Add(lbProjects.Items[i].Value);
}
}
System.Threading.Thread.Sleep(1000);
Response.Redirect("default.aspx");
}
protected void lbViewProfile_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}
protected void lbLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
protected void btnDeleteProfile_Click(object sender, EventArgs e)
{
try
{
//删除用户配置文件
System.Web.Profile.ProfileManager.DeleteProfile(txtUserName.Text);
txtUserName.Text = "";
lbProjects.SelectedIndex = -1;
cbCanOperateAllBills.Checked = false;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
System.Threading.Thread.Sleep(1000);
}
}
另外,Profile一般会与身份认证一起使用以为各用户存储独立的配置文件。
完整代码请点击http://dl2.csdn.net/down4/20070726/26191341182.rar下载。