webabcd - 专注于asp.net, Silverlight

ASP.NET
从现在开始 一切都不晚
posts - 205, comments - 5700, trackbacks - 594, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
[索引页]
[源码下载]



温故知新ASP.NET 2.0(C#)(7) - Profile(存储用户配置)


作者:webabcd


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


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

    
<anonymousIdentification
      
enabled="true"
      cookieName
=".VS2005_ANONYMOUS"
      cookieTimeout
="1440"
      cookiePath
="/"
      cookieRequireSSL
="false"
      cookieSlidingExpiration
="true"
      cookieProtection
="All"
      cookieless
="UseCookies" />

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

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

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

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


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

3、将匿名配置文件迁移到经过身份验证的配置文件
在global.asax加一个Profile_MigrateAnonymous事件处理,示例如下,仅为说明
    void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
    
{
      
// 获得匿名配置
      ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);

      
// 从匿名配置中取值并赋值给经过身份验证的配置
      if (anonProfile.Color != System.Drawing.Color.Empty)
      
{
        Profile.Color 
= anonProfile.Color;
      }

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


示例
App_Code/CustomProfile.cs
using System;
using System.Data;
using System.Configuration;
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;

using System.Web.Profile;

/// <summary>
/// CustomProfile 的摘要说明
/// </summary>

public class CustomProfile : ProfileBase
{
    
private string _customName;
    
public string CustomName
    
{
        
get return (string)base["CustomName"]; }
        
set base["CustomName"= value; }
    }


    
private bool _customSex;
    
public bool CustomSex
    
{
        
get return (bool)base["CustomSex"]; }
        
set base["CustomSex"= value; }
    }

}


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

Profile/Test.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs"
    Inherits
="Profile_Test" Title="存储用户配置测试" 
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    
<asp:Label ID="lbl" runat="Server" />
</asp:Content>

Profile/Test.aspx.cs
using System;
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 Profile_Test : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
// 一看就懂
        Profile.Name = User.Identity.Name;
        Profile.Color 
= System.Drawing.Color.AliceBlue;
        Profile.Group.Collection.Clear();
        Profile.Group.Collection.Add(
"冰棍");
        Profile.Group.Collection.Add(
"瓜子");
        Profile.Group.Price 
= 999999;

        Profile.CustomName 
= User.Identity.Name;
        Profile.CustomSex 
= true;



        lbl.Text 
= "Name:" + Profile.Name + "<br />";
        lbl.Text 
+= "Color:" + Profile.Color.ToString() + "<br />";
        
foreach (string s in Profile.Group.Collection)
        
{
            lbl.Text 
+= "商品有:" + s + "<br />";
        }

        lbl.Text 
+= "价格:" + Profile.Group.Price + "<br />";

        lbl.Text 
+= "自定义类名字:" + Profile.CustomName + "<br />";
        lbl.Text 
+= "自定义类姓名:" + Profile.CustomSex;
    }

}


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


注:需要用aspnet_regsql配置数据库


OK
[源码下载]
 

Feedback

#1楼   回复  引用  查看    

2007-02-22 22:13 by 张振      
继续支持

#2楼[楼主]   回复  引用  查看    

2007-02-23 19:31 by webabcd      
@张振
呵,都是些比较基础的东西

#3楼   回复  引用    

2007-08-21 12:19 by hmx[未注册用户]
如果是修改数据后要验证是否修改成功,要怎么做呢,怎么验证呢

#4楼[楼主]   回复  引用  查看    

2007-08-21 14:09 by webabcd      
@hmx
如果要修改数据后要验证的话
可以保存后再把数据读取出来啊

#5楼   回复  引用    

2008-02-13 21:45 by 黑白[未注册用户]
搞不懂,这些信息是存放在哪的呢

#6楼[楼主]   回复  引用  查看    

2008-02-13 22:47 by webabcd      
@黑白
如果你用的Provider是System.Web.Profile.SqlProfileProvider的话
那么就是存储在aspnet_Profile表里

#7楼   回复  引用  查看    

2008-02-28 19:28 by 北漂浪子      
遇到个问题(关于Profile的),楼主帮忙分析一下:

Demo中如果将下列语句不放在Page_Load里面
{
Profile.Name = User.Identity.Name;
Profile.Color = System.Drawing.Color.AliceBlue;
Profile.Group.Collection.Clear();
Profile.Group.Collection.Add("冰棍");
Profile.Group.Collection.Add("瓜子");
Profile.Group.Price = 999999;

Profile.CustomName = User.Identity.Name;
Profile.CustomSex = true;
}
而是用一个按钮事件来存储它的Profile(假设Save按钮)
protected void btnSaveProfile_Click(object sender, EventArgs e)
{
Profile.Name = User.Identity.Name;
Profile.Color = System.Drawing.Color.AliceBlue;
Profile.Group.Collection.Clear();
Profile.Group.Collection.Add("冰棍");
Profile.Group.Collection.Add("瓜子");
Profile.Group.Price = 999999;
Profile.CustomName = User.Identity.Name;
Profile.CustomSex = true;
}
这样再获得它的Profile属性值,CustomName 和CustomSex 的值不存在。也就是Profile.CustomName 总等于空字符“”,Profile.CustomSex总等于false。

#8楼[楼主]   回复  引用  查看    

2008-02-28 21:56 by webabcd      
@北漂浪子
我试了一下,没问题啊

#9楼   回复  引用  查看    

2008-02-29 09:07 by 北漂浪子      
@webabcd
如果把保存profile和显示profile的代码分开,就会出现以上我说的情况(如果保存和显示profile写在一起没有任何问题)。
我的测试如下:
前台加了三个按钮
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="保存Profile" />
<asp:Button ID="btnShow" runat="server" OnClick="btnShow_Click" Text="显示Profile" />
<asp:Button ID="btnSaveAndShow" runat="server" OnClick="btnSaveAndShow_Click" Text="保存并显示Profile" />
后台如下:
public partial class Profile_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSave_Click(object sender, EventArgs e)
{
Profile.Name = User.Identity.Name;
Profile.Color = System.Drawing.Color.AliceBlue;
Profile.Group.Collection.Clear();
Profile.Group.Collection.Add("冰棍");
Profile.Group.Collection.Add("瓜子");
Profile.Group.Price = 999999;

Profile.CustomName = User.Identity.Name;
Profile.CustomSex = true;

}
protected void btnShow_Click(object sender, EventArgs e)
{

lbl.Text = "Name:" + Profile.Name + "
";
lbl.Text += "Color:" + Profile.Color.ToString() + "
";
foreach (string s in Profile.Group.Collection)
{
lbl.Text += "商品有:" + s + "
";
}
lbl.Text += "价格:" + Profile.Group.Price + "
";

lbl.Text += "自定义类名字:" + Profile.CustomName + "
";
lbl.Text += "自定义类姓名:" + Profile.CustomSex;

}
protected void btnSaveAndShow_Click(object sender, EventArgs e)
{
Profile.Name = User.Identity.Name;
Profile.Color = System.Drawing.Color.AliceBlue;
Profile.Group.Collection.Clear();
Profile.Group.Collection.Add("冰棍");
Profile.Group.Collection.Add("瓜子");
Profile.Group.Price = 999999;

Profile.CustomName = User.Identity.Name;
Profile.CustomSex = true;

lbl.Text = "Name:" + Profile.Name + "
";
lbl.Text += "Color:" + Profile.Color.ToString() + "
";
foreach (string s in Profile.Group.Collection)
{
lbl.Text += "商品有:" + s + "
";
}
lbl.Text += "价格:" + Profile.Group.Price + "
";

lbl.Text += "自定义类名字:" + Profile.CustomName + "
";
lbl.Text += "自定义类姓名:" + Profile.CustomSex;
}

}

先单击"保存Profile”,后击"显示Profile",结果如下:
Name:abc
Color:Color [Empty]
商品有:冰棍
商品有:瓜子
价格:999999
自定义类名字:
自定义类姓名:False

如果直接单击"保存并显示Profile",结果:
Name:abc
Color:Color [AliceBlue]
商品有:冰棍
商品有:瓜子
价格:999999
自定义类名字:abc
自定义类姓名:True

这两个结果有很大的不同,请问如何解释?

#10楼[楼主]   回复  引用  查看    

2008-02-29 17:50 by webabcd      
@北漂浪子
嗯,我的代码的问题,已经改过来了,如下

public class CustomProfile : ProfileBase
{
private string _customName;
public string CustomName
{
get { return (string)base["CustomName"]; }
set { base["CustomName"] = value; }
}

private bool _customSex;
public bool CustomSex
{
get { return (bool)base["CustomSex"]; }
set { base["CustomSex"] = value; }
}
}

#11楼   回复  引用  查看    

2008-03-01 12:49 by 北漂浪子      
@webabcd
依然存在一个小问题,按照上面我测试的例子,Color两次显示结果不一样,
1.先单击"保存Profile”,后击"显示Profile",结果是:
Color:Color [Empty]
2.直接单击"保存并显示Profile",结果是:
Color:Color [AliceBlue]
为什么1中读取出来的profile中Color的值为Empty?

#12楼[楼主]   回复  引用  查看    

2008-03-01 22:30 by webabcd      
@北漂浪子
嗯,发现了,我想可能是Profile不支持System.Drawing.Color类型



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 653849




相关文章:

相关链接: