關鍵
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; www.2cto.com
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
作者:niemeiquan