溫故知新ASP.NET 2.0(C#)(7) - Profile(存儲使用者配置)
介紹
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>
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.Collections;
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配置資料庫
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344887,如需轉載請自行聯系原作者