`
jiagou
  • 浏览: 2533622 次
文章分类
社区版块
存档分类
最新评论

C#_Profile 配置

 
阅读更多
关键

  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" />

<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

分享到:
评论

相关推荐

    c# 操作 ini 配置文件

    c# 操作 ini 配置文件 读取,写配置

    Java SpringBoot课件+源码视频教程

    16、_SpringBoot_配置-Profile多环境支持 ( h, f6 Y' s! ]9 v! _5 J% ^+ `, ^ 17、_SpringBoot_配置-配置文件的加载位置 18、_SpringBoot_配置-外部配置加载顺序 19、_SpringBoot_配置-自动配置原理 6 o* r% s4 }/...

    aspnet-webapi-azure-deploy:ASP.NET Web API Azure部署

    ASP.NET Web API Azure部署 通过GitHub动作将ASP.NET Web API部署到Azure Web应用程序。 脚步: 使用两个新的API和一个Web应用程序创建一个新的解决方案。... publish-profile: ${{ secrets.AZURE_

    visual C# 2005 实例

    ProfileWebSite 使用Profile个性定义的实例。 &lt;br&gt;第15章(\C15) 示例描述:本章介绍.NET 2.0提供的个性UI。 MasterPageSample 使用母版页的案例。 NestedMasterPage 母版页嵌套的应用案例...

    [源代码] ASP.NET 3.5 网站开发全程解析 (C#版本)

    - 用户管理:成员(Membership)、用户配置(Profile)应用; - 本地化: (Localization)应用; - 新闻文章(Article)模块; - 民意调查(PollBox)模块; - 新闻邮件(Newsletter)模块; - 论坛(Forum)模块; - 电子商店...

    圣殿祭司的ASP.NET 4.0专家技术手册,完整扫描版

    ASP.NET 4.0技术概述、ASP.NET程序的编译模型、将ASP.NET程序开发服务器Port固定的技巧、C# 4.0语言新功能、对象初始化程序、LINQ架构概述、LINQ标准查询运算符、跨页发送、ASP.NET网页指令、My对象的内涵、C# 直接...

    aspnet-azure-deploy:通过GitHub操作将ASP.NET应用程序部署到Azure

    ASP.NET应用程序Azure部署 通过GitHub操作将ASP.NET应用程序部署到Azure Web应用程序。 脚步: 创建一个新的ASP.NET Web应用程序。... publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }} package: ${{

    rp-profile-download

    该程序使用的配置文件数据托管在,并采用JSON格式。 该程序将该数据转换为RP Profile Viewer插件可用的LUA表。 RP Profile Viewer插件的文件也包含在存储库中。使用RP Profile Monitor 在操作过程中,Profile ...

    JDK6资源.rar

    JDK6资源,安装流程 1.下载jdk jdk-6u45-linux-x64.zip 2.上传到/usr 3.解压 unzip jdk-6u45-linux-x64.zip 4.配置profile vi /etc/profile

    NewHgrc:用于 PowerShell 的新 Hgrc cmdlet

    将以下内容添加到您的 PowerShell 配置文件~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 : Import-Module New-Hgrc ###3。 将以下内容添加到您的%HOMEPATH%\mercurial.

    gitter:适用于Windows的git本地存储库管理工具

    它完全用C#编写,并针对.NET FW 4.0客户端配置文件。 应用程序主要在Win7上进行了测试,但是应该可以在Vista / XP上正常运行。执照Gitter在GNU GPL3下。 有关完整的许可证文本,请参阅LICENSE文件。储存库访问选项...

    [源代码] ASP.NET 2.0 网站开发全程解析 (源代码)

    - 用户管理:成员(Membership)、用户配置(Profile)应用; - 个性化设置: (Web Part)应用; - 本地化: (Localization)应用; - 论坛(Forum)模块; - 电子商店(Store)模块; - 新闻文章(Article)模块; - 民意调查...

    asp.net知识库

    多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本集合 ASP.NET 中的正则表达式 常用的匹配正则表达式和实例 经典正则表达式 delegate vs. ...

    AspectedRouting:轻松构建路由配置文件

    这些信息在一个profile中分组在一起,该profile使用这些功能来说明行为-例如,车辆是否可以进入一条道路,可以双向行驶还是只能双向行驶,实际行驶的速度有多快,以及最终的“最佳”状态如何?路是。 使用...

    MonitorSwitcher:将自定义GUI添加到现有的监视器切换器应用程序

    新的功能Winforms GUI在窗体上显示热键以及配置文件即将推出的小功能重命名个人资料的能力重新配置个人资料的能力能够重置所有配置文件即将推出的大功能能够最小化到托盘并通过上下文菜单进行操作能够将整个配置文件...

    ASP.NET2.0典型模块(17-22)ASP.NET2.0典型模块(1-16)

    ProfileWebSite\SimpleProfile.aspx 简单个性配置设置 ProfileWebSite\ComplexProfile.aspx 复杂个性配置设置 ProfileWebSite\ MigrateAnonymous.aspx 匿名用户的个性配置迁移 &lt;br&gt;第4章(\C04)...

    loadassembly-for-pcl:在配置文件 259 下运行时,允许在 Windows 桌面、Xamarin iOS 和 Xamarin Android 上加载运行时程序集

    )PCL 配置文件中, Assembly类对运行时程序集加载的支持有限; 具体来说,它只支持按名称从 GAC 加载。 该库为在portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10下运行的库Assembly.Load...

    ASPNET-CORE-RADAR

    基本的 Core模型视图控制器(MVC)应用程序,基于C#构建,具有面向方面的编程(AOP)。 特征 用户认证 具有预定义角色和用户特定权限的用户授权 使用Cookie的会话控件 上面的所有功能均在AOP中实现,而无需使用任何...

    Mathesh099:我的GitHub个人资料的配置文件

    C# HTML CSS Java脚本 SQL Power BI 画面 Qlikview 项目: 前端 这是我第一个使用基本HTML和CSS的网页项目。 通过单击下面的gif,它将把您定向到项目所在的前端存储库(分支:project-1)。

    nvidiaProfileInspector

    NVIDIA Profile Inspector 此工具用于在nvidia驱动程序的内部驱动程序数据库内修改游戏配置文件。 所有游戏配置文件均由nvidia驱动程序提供,但是您可以为驱动程序数据库中缺少的游戏添加自己的配置文件。 您还可以...

Global site tag (gtag.js) - Google Analytics