原文地址:http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/UserProfiles.asp
譯者:Tony Qu
介紹
User Profile總攬
定義User Profile
使用Profile組
使用複雜的Profile屬性
繼承一個Profile
遷移匿名Profile設置
配置Profile Provider
管理Profiles並生成Profile報告
總結
相關書籍
本文中,你將學習如何在一個應用中定義user profile。我們也會向你演示如何配置使用不同provider的profile。最後,你將學習如何管理和生成user profile的報告。
Profile 對象與Session對象十分相似,但是更好用一些。與Session相似的地方在於,Profile是相對於一個特定的用戶的,也就是說,每個Web應用程序的用戶都有他們自己的profile對象。與Session不同的是,Profile對象是持久對象。如果你向Session中添加一個項,在你離開網站時,該項就會消失。而Profile則完全不同,當你修改Profile的狀態時,修改在多個訪問之間均有效。
使用強類型是有它的道理的。例如,使用強類型,你就可以在Microsoft Visual Web Developer中使用智能感知技術,當你鍵入Profile和一個點的時候,智能感知會彈出你已經定義過的profile屬性列表。
你既可以在machine.config中,也可以在web.config中定義一個user profile,由於你不能在應用程序的二級目錄中創建一個包含文件profile節的web.config文件,這意味着你將無法在一個應用程序中定義兩個以上的profile。
在列表1的web.config中,列舉了一個簡單的profile定義的實例,該profile有三個屬性,FirstName, LastName和PageVisits。
<configuration>
<system.web>
<authentication mode="Forms" />
<anonymousIdentification enabled="true" />
<profile>
<properties>
<add
name="FirstName"
defaultValue="??"
allowAnonymous="true" />
<add
name="LastName"
defaultValue="??"
allowAnonymous="true" />
<add
name="PageVisits"
type="Int32"
allowAnonymous="true"/>
</properties>
</profile>
</system.web>
</configuration>
由於該profile需要同時被匿名用戶和已認證用戶使用,因此我們在web.config文件中增加包含一個< anonymousIdentification>元素,有了這個元素,系統就會自動爲匿名用戶生成唯一的ID。仔細看的話我們會發現,每一個 profile屬性都有一個allowAnonymous特性,該特性表明這個profile屬性是否允許被匿名用戶使用。
當你定義好一個profile之後,系統會自動在下一次頁面被調用時,生成一個與該profile相對應的類。這個類會被保存在"Temporary ASP.NET Files Directory"目錄(該目錄也用於存放用於動態生成頁面的類)。你可以使用HttpContext的Profile屬性(Property)調用該類。
當你定義好一個profile後,你可以使用如下方法爲profile屬性賦值。
Profile.FirstName = "Bill"
[C#]
Profile.FirstName = "Bill";
列表2演示了你該如何使用profile來持久化保存用戶信息。這個頁顯示了FirstName,LastName, PageVisits三個屬性的值,同時它包含了一個能夠用於修改這三個屬性的表單(form)。在Page_Load中更新PageVisits的值,這意味着每一次刷新頁面,PageVisits的值都會改變。
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
Profile.PageVisits += 1
End Sub
Sub UpdateProfile(ByVal s As Object, ByVal e As EventArgs)
Profile.FirstName = txtFirstName.Text
Profile.LastName = txtLastName.Text
End Sub
</script>
<html>
<head>
<title>Simple</title>
</head>
<body>
<form id="form1" runat="server">
<b>Name:</b> <%= Profile.FirstName %> <%= Profile.LastName %>
<br />
<b>Page Visits:</b> <%= Profile.PageVisits %>
<hr />
<b>First Name:</b>
<asp:TextBox ID="txtFirstName" Runat="Server" />
<br />
<b>Last Name:</b>
<asp:TextBox ID="txtLastName" Runat="Server" />
<br />
<asp:Button
Text="Update Profile"
OnClick="UpdateProfile"
Runat="server" />
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load() {
Profile.PageVisits ++;
}
void UpdateProfile(Object s, EventArgs e) {
Profile.FirstName = txtFirstName.Text;
Profile.LastName = txtLastName.Text;
}
</script>
<html>
<head>
<title>Simple</title>
</head>
<body>
<form id="form1" runat="server">
<b>Name:</b> <%= Profile.FirstName %> <%= Profile.LastName %>
<br />
<b>Page Visits:</b> <%= Profile.PageVisits %>
<hr />
<b>First Name:</b>
<asp:TextBox ID="txtFirstName" Runat="Server" />
<br />
<b>Last Name:</b>
<asp:TextBox ID="txtLastName" Runat="Server" />
<br />
<asp:Button ID="Button1"
Text="Update Profile"
OnClick="UpdateProfile"
Runat="server" />
</form>
</body>
</html>
<configuration>
<system.web>
<anonymousIdentification enabled="true" />
<profile>
<properties>
<group name="Address">
<add
name="Street"
allowAnonymous="true" />
<add
name="City"
allowAnonymous="true" />
</group>
<group name="Preferences">
<add
name="ReceiveNewsletter"
type="Boolean"
defaultValue="false"
allowAnonymous="true" />
</group>
</properties>
</profile>
</system.web>
</configuration>
當你用組來定義profile時,你應該使用組名來設置或讀取profile屬性。例如,在列表3中,你可以使用以下一些句子來完成三個profile屬性的賦值。
舉個例子,假設你現在需要在profile中存儲一個購物籃,如果這樣做的話,你就可以在每次訪問網站時獲得自己的購物籃。
列表4 聲明瞭一個包含profile,這個profile包含一個名爲ShoppingCart的屬性,而該屬性的type特性是一個叫ShoppingCart的類(我們接下來會創建該類),該類名是有效的。
我們還會注意到,該聲明中包含一個serializeAs特性,該特性可以幫助ShoppingCart使用二進制序列化器(binary serializer)進行持久化,而不是使用xml序列化器。
<configuration>
<system.web>
<anonymousIdentification enabled="true" />
<profile>
<properties>
<add
name="ShoppingCart"
type="ShoppingCart"
serializeAs="Binary"
allowAnonymous="true" />
</properties>
</profile>
</system.web>
</configuration>
列表5 中有一個簡單購物籃的實現代碼,該購物籃擁有添加和刪除項(item)的方法(method),同時它還擁有兩個屬性(property),一個是用於獲得該購物籃中的所有項的,一個是用於表示所有商品的總價的。
Imports Microsoft.VisualBasic
<Serializable()> _
Public Class ShoppingCart
Public _CartItems As New Hashtable()
' Return all the items from the Shopping Cart
Public ReadOnly Property CartItems() As ICollection
Get
Return _CartItems.Values
End Get
End Property
' The sum total of the prices
Public ReadOnly Property Total() As Decimal
Get
Dim sum As Decimal
For Each item As CartItem In _CartItems.Values
sum += item.Price * item.Quantity
Next
Return sum
End Get
End Property
' Add a new item to the shopping cart
Public Sub AddItem(ByVal ID As Integer, _
ByVal Name As String, ByVal Price As Decimal)
Dim item As CartItem = CType(_CartItems(ID), CartItem)
If item Is Nothing Then
_CartItems.Add(ID, New CartItem(ID, Name, Price))
Else
item.Quantity += 1
_CartItems(ID) = item
End If
End Sub
' Remove an item from the shopping cart
Public Sub RemoveItem(ByVal ID As Integer)
Dim item As CartItem = CType(_CartItems(ID), CartItem)
If item Is Nothing Then
Return
End If
item.Quantity -= 1
If item.Quantity = 0 Then
_CartItems.Remove(ID)
Else
_CartItems(ID) = item
End If
End Sub
End Class
<Serializable()> _
Public Class CartItem
Private _ID As Integer
Private _Name As String
Private _Price As Decimal
Private _Quantity As Integer = 1
Public ReadOnly Property ID() As Integer
Get
Return _ID
End Get
End Property
Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property
Public ReadOnly Property Price() As Decimal
Get
Return _Price
End Get
End Property
Public Property Quantity() As Integer
Get
Return _Quantity
End Get
Set(ByVal value As Integer)
_Quantity = value
End Set
End Property
Public Sub New(ByVal ID As Integer, _
ByVal Name As String, ByVal Price As Decimal)
_ID = ID
_Name = Name
_Price = Price
End Sub
End Class
using System;
using System.Collections;
[Serializable]
public class ShoppingCart
{
public Hashtable _CartItems = new Hashtable();
// Return all the items from the Shopping Cart
public ICollection CartItems
{
get { return _CartItems.Values; }
}
// The sum total of the prices
public decimal Total
{
get
{
decimal sum = 0;
foreach (CartItem item in _CartItems.Values)
sum += item.Price * item.Quantity;
return sum;
}
}
// Add a new item to the shopping cart
public void AddItem(int ID, string Name, decimal Price)
{
CartItem item = (CartItem)_CartItems[ID];
if (item == null)
_CartItems.Add(ID, new CartItem(ID, Name, Price));
else
{
item.Quantity++;
_CartItems[ID] = item;
}
}
// Remove an item from the shopping cart
public void RemoveItem(int ID)
{
CartItem item = (CartItem)_CartItems[ID];
if (item == null)
return;
item.Quantity--;
if (item.Quantity == 0)
_CartItems.Remove(ID);
else
_CartItems[ID] = item;
}
}
[Serializable]
public class CartItem
{
private int _ID;
private string _Name;
private decimal _Price;
private int _Quantity = 1;
public int ID
{
get { return _ID; }
}
public string Name
{
get { return _Name; }
}
public decimal Price
{
get { return _Price; }
}
public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
public CartItem(int ID, string Name, decimal Price)
{
_ID = ID;
_Name = Name;
_Price = Price;
}
}
如果你把列表5中的代碼添加到應用程序的App_Code目錄中,購物籃會自動被編譯。
圖2 在profile中存儲購物籃
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
If Not IsPostBack Then
BindShoppingCart()
End If
End Sub
Sub BindShoppingCart()
If Not Profile.ShoppingCart Is Nothing Then
CartGrid.DataSource = Profile.ShoppingCart.CartItems
CartGrid.DataBind()
lblTotal.Text = Profile.ShoppingCart.Total.ToString("c")
End If
End Sub
Sub AddCartItem(ByVal s As Object, ByVal e As EventArgs)
Dim row As GridViewRow = ProductGrid.SelectedRow
Dim ID As Integer = CInt(ProductGrid.SelectedDataKey.Value)
Dim Name As String = row.Cells(1).Text
Dim Price As Decimal = CDec(row.Cells(2).Text)
If Profile.ShoppingCart Is Nothing Then
Profile.ShoppingCart = New ShoppingCart
End If
Profile.ShoppingCart.AddItem(ID, Name, Price)
BindShoppingCart()
End Sub
Sub RemoveCartItem(ByVal s As Object, ByVal e As EventArgs)
Dim ID As Integer = CInt(CartGrid.SelectedDataKey.Value)
Profile.ShoppingCart.RemoveItem(ID)
BindShoppingCart()
End Sub
</script>
<html>
<head>
<title>Products</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%">
<tr>
<td valign="top">
<h2>Products</h2>
<asp:GridView
ID="ProductGrid"
DataSourceID="ProductSource"
DataKeyNames="ProductID"
AutoGenerateColumns="false"
OnSelectedIndexChanged="AddCartItem"
ShowHeader="false"
CellPadding="5"
Runat="Server">
<Columns>
<asp:ButtonField
CommandName="select"
Text="Buy" />
<asp:BoundField
DataField="ProductName" />
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="ProductSource"
ConnectionString=
"Server=localhost;Database=Northwind;Trusted_Connection=true;"
SelectCommand=
"SELECT ProductID,ProductName,UnitPrice FROM Products"
Runat="Server" />
</td>
<td valign="top">
<h2>Shopping Cart</h2>
<asp:GridView
ID="CartGrid"
AutoGenerateColumns="false"
DataKeyNames="ID"
OnSelectedIndexChanged="RemoveCartItem"
CellPadding="5"
Width="300"
Runat="Server">
<Columns>
<asp:ButtonField
CommandName="select"
Text="Remove" />
<asp:BoundField
DataField="Name"
HeaderText="Name" />
<asp:BoundField
DataField="Price"
HeaderText="Price"
DataFormatString="{0:c}" />
<asp:BoundField
DataField="Quantity"
HeaderText="Quantity" />
</Columns>
</asp:GridView>
<b>Total:</b>
<asp:Label ID="lblTotal" Runat="Server" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
void Page_Load() {
if (!IsPostBack)
BindShoppingCart();
}
void BindShoppingCart()
{
if (Profile.ShoppingCart != null)
{
CartGrid.DataSource = Profile.ShoppingCart.CartItems;
CartGrid.DataBind();
lblTotal.Text = Profile.ShoppingCart.Total.ToString("c");
}
}
void AddCartItem(Object s, EventArgs e)
{
GridViewRow row = ProductGrid.SelectedRow;
int ID = (int)ProductGrid.SelectedDataKey.Value;
String Name = row.Cells[1].Text;
decimal Price = Decimal.Parse(row.Cells[2].Text,
NumberStyles.Currency);
if (Profile.ShoppingCart == null)
Profile.ShoppingCart = new ShoppingCart();
Profile.ShoppingCart.AddItem(ID, Name, Price);
BindShoppingCart();
}
void RemoveCartItem(Object s, EventArgs e)
{
int ID = (int)CartGrid.SelectedDataKey.Value;
Profile.ShoppingCart.RemoveItem(ID);
BindShoppingCart();
}
</script>
<html>
<head>
<title>Products</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%">
<tr>
<td valign="top">
<h2>Products</h2>
<asp:GridView
ID="ProductGrid"
DataSourceID="ProductSource"
DataKeyNames="ProductID"
AutoGenerateColumns="false"
OnSelectedIndexChanged="AddCartItem"
ShowHeader="false"
CellPadding="5"
Runat="Server">
<Columns>
<asp:ButtonField
CommandName="select"
Text="Buy" />
<asp:BoundField
DataField="ProductName" />
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="ProductSource"
ConnectionString=
"Server=localhost;Database=Northwind;Trusted_Connection=true;"
SelectCommand=
"SELECT ProductID,ProductName,UnitPrice FROM Products"
Runat="Server" />
</td>
<td valign="top">
<h2>Shopping Cart</h2>
<asp:GridView
ID="CartGrid"
AutoGenerateColumns="false"
DataKeyNames="ID"
OnSelectedIndexChanged="RemoveCartItem"
CellPadding="5"
Width="300"
Runat="Server">
<Columns>
<asp:ButtonField
CommandName="select"
Text="Remove" />
<asp:BoundField
DataField="Name"
HeaderText="Name" />
<asp:BoundField
DataField="Price"
HeaderText="Price"
DataFormatString="{0:c}" />
<asp:BoundField
DataField="Quantity"
HeaderText="Quantity" />
</Columns>
</asp:GridView>
<b>Total:</b>
<asp:Label ID="lblTotal" Runat="Server" />
</td>
</tr>
</table>
</form>
</body>
</html>
繼承一個profile
你也可以通過從一個已經存在的profile類中繼承一個profile來完成對profile的定義,這種特性能夠幫助你在多個應用程序中使用相同的profile。
例如,列表7中列出了一個擁有多個用戶屬性的類,該類是從ProfileBase類繼承而來的(你可以在System.Web.Profile中找到)
Imports Microsoft.VisualBasic
Imports System.Web.Profile
Public Class UserInfo
Inherits ProfileBase
Private _FirstName As String
Private _LastName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
End Set
End Property
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
End Set
End Property
End Class
using System;
using System.Web.Profile;
public class UserInfo : ProfileBase
{
private string _FirstName;
private string _LastName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
using System;
using System.Web.Profile;
public class UserInfo : ProfileBase
{
private string _FirstName;
private string _LastName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
<configuration>
<system.web>
<anonymousIdentification enabled="true" />
<profile inherits="UserInfo" />
</system.web>
</configuration>
遷移匿名Profile設置
Profile對象既可用於匿名用戶也可以用於已認證用戶。然而,當用戶從匿名用戶狀態轉換爲已認證用戶狀態時,Profile對象能夠以一種令人難以理解的方式完成任務。
當匿名用戶使用Profile對象時,用戶profile是與一個隨機生成的號碼相關聯的,該號碼是根據每個用戶唯一生成的,它保存在瀏覽器的cookie中,無論何時該用戶返回應用程序,該用戶的Profile設置會被自動加載。
如果匿名用戶通過認證的話,所有與該用戶相關的profile就會丟失,同時系統會生成一個新的profile。這時該Profile信息將與用戶名相關聯,而非唯一識別號。
要想理解所有這些工作,最好的方法就是看看下面的例子。列表9中的web.config定義了一個profile,該profile只有一個FavoriteColor屬性。
<configuration>
<system.web>
<authentication mode="Forms" />
<anonymousIdentification enabled="true" />
<profile>
<properties>
<add
name="FavoriteColor"
allowAnonymous="true"
defaultValue="Red" />
</properties>
</profile>
</system.web>
</configuration>
列表10中有一個包含兩個按鈕的頁面,分別是login和logout按鈕,其中還有一個用於更新FavoriteColor屬性的表單。
<%@ Page Language="VB" %>
<script runat="server">
Sub Login(ByVal s As Object, ByVal e As EventArgs)
FormsAuthentication.SetAuthCookie("Bill", False)
Response.Redirect(Request.Path)
End Sub
Sub Logout(ByVal s As Object, ByVal e As EventArgs)
FormsAuthentication.SignOut()
Response.Redirect(Request.Path)
End Sub
Sub UpdateProfile(ByVal s As Object, ByVal e As EventArgs)
Profile.FavoriteColor = txtFavoriteColor.Text
End Sub
Sub Page_PreRender()
lblUsername.Text = Profile.UserName
lblFavoriteColor.Text = Profile.FavoriteColor
End Sub
</script>
<html>
<head>
<title>Anonymous</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1"
Text="Login"
OnClick="Login"
Runat="Server" />
<asp:Button ID="Button2"
Text="Logout"
OnClick="Logout"
Runat="Server" />
<hr />
<asp:TextBox
id="txtFavoriteColor"
Runat="Server" />
<asp:Button ID="Button3"
Text="Update Profile"
OnClick="UpdateProfile"
Runat="Server" />
<hr />
<b>Username:</b>
<asp:Label
id="lblUsername"
Runat="Server" />
<br />
<b>Favorite Color:</b>
<asp:Label
id="lblFavoriteColor"
Runat="Server" />
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
void Login(Object s, EventArgs e)
{
FormsAuthentication.SetAuthCookie("Bill", false);
Response.Redirect(Request.Path);
}
void Logout(Object s, EventArgs e)
{
FormsAuthentication.SignOut();
Response.Redirect(Request.Path);
}
void UpdateProfile(Object s, EventArgs e)
{
Profile.FavoriteColor = txtFavoriteColor.Text;
}
void Page_PreRender()
{
lblUsername.Text = Profile.UserName;
lblFavoriteColor.Text = Profile.FavoriteColor;
}
</script>
<html>
<head>
<title>Anonymous</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button
Text="Login"
OnClick="Login"
Runat="Server" />
<asp:Button ID="Button1"
Text="Logout"
OnClick="Logout"
Runat="Server" />
<hr />
<asp:TextBox
id="txtFavoriteColor"
Runat="Server" />
<asp:Button
Text="Update Profile"
OnClick="UpdateProfile"
Runat="Server" />
<hr />
<b>Username:</b>
<asp:Label
id="lblUsername"
Runat="Server" />
<br />
<b>Favorite Color:</b>
<asp:Label
id="lblFavoriteColor"
Runat="Server" />
</form>
</body>
</html>
當你打開第一個頁面時,UserName的值是一個隨機生成的唯一識別號(見圖3)。當你按下Login按鈕後,你就完成了身份認證,它是通過用戶票據(User Bill)完成的。
圖3 使用匿名和認證profile
列表10的頁面中包含一個用於更新FavoriteColor的表單,要注意的是,在你登錄登出的時候,會分別生成兩個不同的profile。例如當你先登錄,後登出的話,那麼系統會生成一個隨機的唯一識別號。
在很多情況下,你需要把匿名profile遷移到認證profile狀態,如果你需要遷移profile屬性值的話,你可以利用 ProfileModule類的MigrateAnonymous事件完成該任務,該事件只能在Global.asax文件中進行處理。列表11中的 Global.asax演示了你如何才能實現FavoriteColor屬性的遷移。
<%@ Application Language="VB" %>
<script runat="server">
Sub Profile_MigrateAnonymous(ByVal s As Object, _
ByVal e As ProfileMigrateEventArgs)
Dim anonProfile As ProfileCommon = _
Profile.GetProfile(e.AnonymousId)
Profile.FavoriteColor = anonProfile.FavoriteColor
End Sub
</script>
<%@ Application Language="C#" %>
<script runat="server">
void Profile_MigrateAnonymous(Object s,
ProfileMigrateEventArgs e)
{
ProfileCommon anonProfile =
Profile.GetProfile(e.AnonymousId);
Profile.FavoriteColor = anonProfile.FavoriteColor;
}
</script>
通過Profile類的GetProfile()方法你可以獲得匿名profile,該方法接收一個唯一識別號,並且返回與唯一識別號對應的profile。ProfileMigrateEventArgs對象包含一個匿名識別號。
配置Profile Provider
默認情況下,profile被保存在sqlserver 2005 express數據庫,它位於App_Data目錄中,這或許在你開發一些普通的asp.net應用程序時是沒有問題的,但很有可能,你需要把你的應用程序的profile保存在另一個數據庫中,比如一個完整版的SqlServer 2005的實例中,而該數據庫又位於你局域網的某個位置。
Profile使用Provider模式,通過修改web.config或machine.config的設置來告訴系統把信息存儲在哪裏。
ASP.NET本身配了一個profile provider,叫SqlProfileProvider。如果你感到困惑,你可以通過繼承ProfileProvider基類來創建一個自己的 provider。例如,你可以創建一個基於Oracle數據庫或MySql數據庫的Provider。在這裏,我們將只討論最簡單的方法,即通過SqlServer數據庫來保存profile信息。
要使用Microsoft SQL Server存儲profile信息,必須完成兩個步驟。首先,你必須安裝SQL Server數據庫,然後你必須重新設置配置文件。
ASP.NET 2.0框架提供了一個用於配置SQL Server來存儲Profile信息的工具,該工具叫做aspnet_regsql,它位於Windows/Microsoft.NET/ Framework/[.NET版本號]。執行該工具後,你會看到圖4中的ASP.NET SQL Server安裝嚮導。
圖4 使用ASP.NET SQL Server安裝程序
SQL Server安裝嚮導會指導你完成必要的步驟,完成這些步驟後,嚮導會自動創建用於存儲profile信息的存儲過程和表結構。
在你完成SQL Server數據庫的配置後,你需要修改web.config或machine.config中的數據庫連接設置來指向服務器上的SQL Server數據庫,本例中該數據庫的實例名爲MyServer,列表12列出了該配置文件。
<configuration>
<connectionStrings>
<add
name="myConnectionString"
connectionString=
"Server=MyServer;Trusted_Connection=true;database=MyDatabase" />
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" />
<profile defaultProvider="MyProfileProvider">
<providers>
<add
name="MyProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="myConnectionString" />
</providers>
<properties>
<add
name="FirstName"
allowAnonymous="true" />
<add
name="LastName"
allowAnonymous="true" />
</properties>
</profile>
</system.web>
</configuration>
在列表12中的profile配置中,包含了一個defaultProvider特性,這個特性指向一個叫MyProfileProvider的 profile provider,而這個provider定義是在profile標記的<providers>節中完成的。 MyProfileProvider則使用一個叫MyConnectionString的連接字符串完成數據庫連接,並保存profile信息到數據庫中。MyConnectionString可以在位於web.config開頭的<connectionStrings>節中找到。
管理profile並生成profile報告
Profile會對象自動保存用戶profile信息,這既是好事業是壞事。說它是好事,是因爲你不需要寫存儲信息的所有邏輯代碼,說它是壞事,是因爲這樣可能造成一大堆無用的信息被保存在數據庫中。
幸運的是,ASP.NET 2.0框架包含一個叫做ProfileManager的類,你可以使用它來管理profile信息。它包含了相當多的方法使你能夠有效地管理profile並且生成profile報表,下面列出了一些該類的重要方法:
- DeleteInactiveProfiles. 刪除一個特定日期之前的所有profile
- DeleteProfile. 刪除特定用戶的profile
- DeleteProfiles. 刪除一個profile集合
- FindInactiveProfilesByUserName. 返回一個ProfileInfo對象的集合,該集合表示的profile是匹配一個某個名字,並且是從某個特定日期開始一直未被使用
- FindProfilesByUserName. 返回一個ProfileInfo對象集合,該集合與某個用戶名相關聯
- GetAllInactiveProfiles. 返回一個ProfileInfo對象集合,該集合表示的profile是從某個特定日期開始一直未被使用的profile
- GetAllProfiles. 返回一個ProfileInfo對象集合,該集合表示所有的profile
- GetNumberOfInactiveProfiles. 返回從某個特定日期開始一直未被使用的profile的數量
- GetNumberOfProfiles. 返回profile總數
- IsAnonymous. 表示該profile是否爲匿名profile
- LastActivityDate. 最後一次profile被訪問的時間和日期
- LastUpdatedDate. 最後一次profile被升級的時間和日期
- Size. 表示profile的大小,這是在profile provider存儲profile信息時記錄的
- UserName. 與profile關聯的用戶名
ProfileManager既可以在asp.net頁面下使用,也可以在其它程序中使用。例如,你可能需要做一個控制檯程序用於每天清除長時間未使用的 profile。列表14的控制檯程序會刪除七天未使用的profile,你可以使用Windows計劃任務(Windows Scheduled Tasks)來安排該程序的執行時間。
Imports System.Web.Profile
Public Class DeleteInactiveProfiles
Public Shared Sub Main()
Dim deleted As Integer
deleted =
ProfileManager.DeleteInactiveProfiles(
ProfileAuthenticationOption.All,
DateTime.Now.AddDays(-7))
Console.WriteLine("Deleted " & deleted & " profiles" )
End Sub
End Class
using System;
using System.Web.Profile;
public class DeleteInactiveProfiles
{
public static void Main()
{
int deleted = 0;
deleted =
ProfileManager.DeleteInactiveProfiles(
ProfileAuthenticationOption.All,
DateTime.Now.AddDays(-7));
Console.WriteLine("Deleted " +
deleted.ToString() + " profiles" );
}
}
你可以通過一下的命令行指令對列表14進行編譯
C:/WINDOWS/Microsoft.NET/Framework/v2.0.40607/vbc
/r:System.Web.dll DeleteInactiveProfiles.vb
[C#]
C:/WINDOWS/Microsoft.NET/Framework/v2.0.40607/csc
DeleteInactiveProfiles.cs
你還可以使用ProfileManager類生成profile信息報表。例如,如果你打算生成一個用戶調查的報表,你可以把用戶調查保存在profile中,這樣就可以輕易的使用ProfileManager生成你需要的報表。
列表15中的web.config中有三個屬性:SurveyCompleted、FavoriteLanguage 和 FavoriteEnvironment
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<anonymousIdentification enabled="true" />
<profile>
<properties>
<add
name="SurveyCompleted"
type="Boolean"
allowAnonymous="true" />
<add
name="FavoriteLanguage"
allowAnonymous="true" />
<add
name="FavoriteEnvironment"
allowAnonymous="true" />
</properties>
</profile>
</system.web>
</configuration>
列表16中的頁面演示了一個簡單的用戶調查。該頁面包含兩個Panel控件,第一個控件中有兩個調查問題,當用戶完成調查後,第一個控件會自動隱藏,而第二個會顯示出來,第二個Panel有一段表示感謝的文字。
<%@ Page Language="VB" %>
<script runat="server">
Sub SaveSurvey(ByVal s As Object, ByVal e As EventArgs)
Profile.FavoriteLanguage = rdlLanguage.SelectedItem.Text
Profile.FavoriteEnvironment = rdlEnvironment.SelectedItem.Text
Profile.SurveyCompleted = True
End Sub
Sub Page_PreRender()
If Profile.SurveyCompleted Then
pnlSurvey.Visible = False
pnlSurveyCompleted.Visible = True
Else
pnlSurvey.Visible = True
pnlSurveyCompleted.Visible = False
End If
End Sub
</script>
<html>
<head>
<title>Survey</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="pnlSurvey" Runat="Server">
What is your favorite programming language?
<br />
<asp:RadioButtonList
id="rdlLanguage"
runat="Server">
<asp:ListItem Text="VB.NET" Selected="True" />
<asp:ListItem Text="C#" />
<asp:ListItem Text="J#" />
</asp:RadioButtonList>
<p> </p>
What is your favorite development environment?
<br />
<asp:RadioButtonList
id="rdlEnvironment"
runat="Server">
<asp:ListItem Text="VS.NET" Selected="True" />
<asp:ListItem Text="Web Matrix" />
<asp:ListItem Text="Notepad" />
</asp:RadioButtonList>
<p> </p>
<asp:Button
Text="Submit Survey"
Onclick="SaveSurvey"
Runat="Server" />
</asp:Panel>
<asp:Panel ID="pnlSurveyCompleted" Runat="Server">
Thank you for completing the survey!
</asp:Panel>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
void SaveSurvey(Object s, EventArgs e)
{
Profile.FavoriteLanguage = rdlLanguage.SelectedItem.Text;
Profile.FavoriteEnvironment = rdlEnvironment.SelectedItem.Text;
Profile.SurveyCompleted = true;
}
void Page_PreRender()
{
if (Profile.SurveyCompleted)
{
pnlSurvey.Visible = false;
pnlSurveyCompleted.Visible = true;
}
else
{
pnlSurvey.Visible = true;
pnlSurveyCompleted.Visible = false;
}
}
</script>
<html>
<head>
<title>Survey</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="pnlSurvey" Runat="Server">
What is your favorite programming language?
<br />
<asp:RadioButtonList
id="rdlLanguage"
runat="Server">
<asp:ListItem Text="VB.NET" Selected="True" />
<asp:ListItem Text="C#" />
<asp:ListItem Text="J#" />
</asp:RadioButtonList>
<p> </p>
What is your favorite development environment?
<br />
<asp:RadioButtonList
id="rdlEnvironment"
runat="Server">
<asp:ListItem Text="VS.NET" Selected="True" />
<asp:ListItem Text="Web Matrix" />
<asp:ListItem Text="Notepad" />
</asp:RadioButtonList>
<p> </p>
<asp:Button ID="Button1"
Text="Submit Survey"
Onclick="SaveSurvey"
Runat="Server" />
</asp:Panel>
<asp:Panel ID="pnlSurveyCompleted" Runat="Server">
Thank you for completing the survey!
</asp:Panel>
</form>
</body>
</html>
列表17中顯示調查的結果,該頁面中有一個顯示ProfileInfo對象集合的GridView控件,該ProfileInfo對象集合是由 ProfileManager的GetAllProfiles方法獲得的。當你點擊GridView中的任意一行的Select鏈接時,你將會看到對這個問題的調查結果,該調查結果是由Profile類的GetProfile方法獲得的。
圖5 顯示調查結果
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()
ResultsGrid.DataSource = _
ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All)
ResultsGrid.DataBind()
End Sub
Sub DisplayProfileDetails(ByVal s As Object, ByVal e As EventArgs)
Dim SelectedProfile As ProfileCommon
SelectedProfile = Profile.GetProfile(ResultsGrid.SelectedValue)
lblLanguage.Text = SelectedProfile.FavoriteLanguage
lblEnvironment.Text = SelectedProfile.FavoriteEnvironment
End Sub
</script>
<html>
<head>
<title>Survey Results</title>
</head>
<body>
<form id="form1" runat="server">
<h2>Survey Results</h2>
<asp:GridView
id="ResultsGrid"
DataKeyNames="UserName"
AutoGenerateSelectButton="true"
OnSelectedIndexChanged="DisplayProfileDetails"
SelectedRowStyle-BackColor="LightYellow"
Runat="Server" />
<p> </p>
<h2>Survey Details</h2>
<b>Favorite Language:</b>
<asp:Label
id="lblLanguage"
Runat="Server" />
<br />
<b>Favorite Environment:</b>
<asp:Label
id="lblEnvironment"
Runat="Server" />
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
ResultsGrid.DataSource =
ProfileManager.GetAllProfiles(ProfileAuthenticationOption.All);
ResultsGrid.DataBind();
}
void DisplayProfileDetails(Object s, EventArgs e)
{
ProfileCommon SelectedProfile =
Profile.GetProfile(ResultsGrid.SelectedValue.ToString());
lblLanguage.Text = SelectedProfile.FavoriteLanguage;
lblEnvironment.Text = SelectedProfile.FavoriteEnvironment;
}
</script>
<html>
<head>
<title>Survey Results</title>
</head>
<body>
<form id="form1" runat="server">
<h2>Survey Results</h2>
<asp:GridView
id="ResultsGrid"
DataKeyNames="UserName"
AutoGenerateSelectButton="true"
OnSelectedIndexChanged="DisplayProfileDetails"
SelectedRowStyle-BackColor="LightYellow"
Runat="Server" />
<p> </p>
<h2>Survey Details</h2>
<b>Favorite Language:</b>
<asp:Label
id="lblLanguage"
Runat="Server" />
<br />
<b>Favorite Environment:</b>
<asp:Label
id="lblEnvironment"
Runat="Server" />
</form>
</body>
</html>
總結
當建立Web應用程序時,我依舊花費了大量的時間和精力用於做一些大傷腦筋的事情。其中的一個任務就是寫一些用於從數據庫存儲和獲得用戶信息的代碼。雖然 Profile對象引入的都是asp.net 1.0中可以實現的功能,但是這個新特性幫助我們從乏味的編碼工作中解脫出來,這樣也能讓我們在寫Web應用程序的過程中,把更多的精力放在我們更感興趣的事情上。
相關書籍
- ASP.NET 2.0 初探 (A First Look at ASP.NET V. 2.0)
- ASP.NET 2.0 測試版預覽(ASP.NET 2.0 Beta Preview)
- Microsoft ASP.NET 2.0開發指南(Introducing Microsoft ASP.NET 2.0)
- ASP.NET技術內幕 (ASP.NET Unleashed)
http://www.cnblogs.com/tonyqus/archive/2005/12/18/292081.html
Profile.Address.City = "Modesto"
Profile.Address.Street = "111 King Arthur Ln"
Profile.Preferences.ReceiveNewsletter = False
[C#]
Profile.Address.City = "Modesto";
Profile.Address.Street = "111 King Arthur Ln";
Profile.Preferences.ReceiveNewsletter = false;