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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

技术小胖子 2017-11-14 02:47:00 浏览625
展开阅读全文
[索引页]
[源码下载]



温故知新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事件,示例如下,仅为说明
InBlock.gifvoid Profile_ProfileAutoSaving(Object sender, ProfileAutoSaveEventArgs e) 
InBlock.gif        { 
InBlock.gif                if ((e.Context.Items["CancelProfileAutoSave"] != null) && ((bool)e.Context.Items["CancelProfileAutoSave"] == true)) 
InBlock.gif                        e.ContinueWithProfileAutoSave = false
InBlock.gif        }
在需要取消Profile的自动保存功能的页的代码处如下写
InBlock.gifprotected void Page_Load(object sender, EventArgs e) 
InBlock.gif
InBlock.gif    Context.Items["CancelProfileAutoSave"] = true;         
InBlock.gif}
 
 
2、通过ProfileManager执行相关任务,如搜索有关所有配置文件、经过身份验证用户的配置文件及匿名用户的配置文件的统计信息,确定在给定时间段内尚未修改的配置文件的数量,根据配置文件的上一次修改日期删除单个配置文件及多个配置文件等

3、将匿名配置文件迁移到经过身份验证的配置文件
在global.asax加一个Profile_MigrateAnonymous事件处理,示例如下,仅为说明
InBlock.gifvoid Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe) 
InBlock.gif        { 
InBlock.gif            // 获得匿名配置 
InBlock.gif            ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID); 
InBlock.gif 
InBlock.gif            // 从匿名配置中取值并赋值给经过身份验证的配置 
InBlock.gif            if (anonProfile.Color != System.Drawing.Color.Empty) 
InBlock.gif            { 
InBlock.gif                Profile.Color = anonProfile.Color; 
InBlock.gif            } 
InBlock.gif                 
InBlock.gif            // 从数据库中删除匿名配置 
InBlock.gif            ProfileManager.DeleteProfile(pe.AnonymousID); 
InBlock.gif                 
InBlock.gif            // 清除与某个会话关联的匿名 Cookie 或标识符 
InBlock.gif            AnonymousIdentificationModule.ClearAnonymousIdentifier();     
InBlock.gif        }
 
示例
App_Code/CustomProfile.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifusing System.Web.Profile; 
InBlock.gif 
/// <summary> 
/// CustomProfile 的摘要说明 
/// </summary> 
InBlock.gifpublic class CustomProfile : ProfileBase 
InBlock.gif
InBlock.gif        private string _customName; 
InBlock.gif        public string CustomName 
InBlock.gif        { 
InBlock.gif                get { return (string)base["CustomName"]; } 
InBlock.gif                set { base["CustomName"] = value; } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private bool _customSex; 
InBlock.gif        public bool CustomSex 
InBlock.gif        { 
InBlock.gif                get { return (bool)base["CustomSex"]; } 
InBlock.gif                set { base["CustomSex"] = value; } 
InBlock.gif        } 
InBlock.gif}
 
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
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class Profile_Test : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.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) 
InBlock.gif                { 
InBlock.gif                        lbl.Text += "商品有:" + s + "<br />"
InBlock.gif                } 
InBlock.gif                lbl.Text += "价格:" + Profile.Group.Price + "<br />"
InBlock.gif 
InBlock.gif                lbl.Text += "自定义类名字:" + Profile.CustomName + "<br />"
InBlock.gif                lbl.Text += "自定义类姓名:" + Profile.CustomSex; 
InBlock.gif        } 
InBlock.gif}
 
用“abc”这个用户登录后的运行结果
Name:abc
Color:Color [AliceBlue]
商品有:冰棍
商品有:瓜子
价格:999999
自定义类名字:abc
自定义类姓名:True


注:需要用aspnet_regsql配置数据库


OK
[源码下载]
 
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344887,如需转载请自行联系原作者






网友评论

登录后评论
0/500
评论
技术小胖子
+ 关注