重新過一遍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 _customName; }
        
set { _customName = value; }
    }


    
private bool _customSex;

    
public bool CustomSex
    
{
        
get return _customSex; }
        
set { _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
[源碼下載]
發佈了19 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章