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

简介:
[索引页]
[源码下载]



温故知新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
[源码下载]
 
 


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






相关文章
|
7月前
|
Java 物联网 C#
C#/.NET/.NET Core学习路线集合,学习不迷路!
C#/.NET/.NET Core学习路线集合,学习不迷路!
331 0
|
2月前
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
111 0
|
6月前
|
开发框架 搜索推荐 算法
一个包含了 50+ C#/.NET编程技巧实战练习教程
一个包含了 50+ C#/.NET编程技巧实战练习教程
209 18
|
6月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
191 12
|
6月前
|
开发框架 人工智能 .NET
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
C#/.NET/.NET Core拾遗补漏合集(24年12月更新)
|
6月前
|
开发框架 算法 .NET
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
C#/.NET/.NET Core技术前沿周刊 | 第 15 期(2024年11.25-11.30)
|
6月前
|
开发框架 Cloud Native .NET
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
C#/.NET/.NET Core技术前沿周刊 | 第 16 期(2024年12.01-12.08)
|
6月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
336 5
|
6月前
|
算法 Java 测试技术
Benchmark.NET:让 C# 测试程序性能变得既酷又简单
Benchmark.NET是一款专为 .NET 平台设计的性能基准测试框架,它可以帮助你测量代码的执行时间、内存使用情况等性能指标。它就像是你代码的 "健身教练",帮助你找到瓶颈,优化性能,让你的应用跑得更快、更稳!希望这个小教程能让你在追求高性能的路上越走越远,享受编程带来的无限乐趣!
258 13
|
7月前
|
Java 物联网 编译器
C#一分钟浅谈:.NET Core 与 .NET 5 区别
本文对比了 .NET Core 和 .NET 5,从历史背景、主要区别、常见问题及易错点等方面进行了详细分析。.NET Core 侧重跨平台支持和高性能,而 .NET 5 在此基础上统一了 .NET 生态系统,增加了更多新特性和优化。开发者可根据具体需求选择合适的版本。
189 7