[转]ASP.NET 2.0个性化配置(profile)系列(1)

为每个用户存储配置信息
强类型 长期保存 支持匿名用户
定义配置(profile
<?xml version="1.0"?>

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    
<appSettings/>
    
<connectionStrings>
        
<add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind" providerName="System.Data.SqlClient"/>
    
</connectionStrings>
    
<system.web>

        
<anonymousIdentification enabled="true" />
        
<profile>   //定义配置(profile)
            
<properties>
                
<add name="Theme" allowAnonymous="true" />
                
<add name="LastVisit" type="System.DateTime" allowAnonymous="true" />
            
</properties>
        
</profile>
<!--
        <webParts>  //定义配置(webParts)
            <personalization defaultProvider="AspNetSqlPersonalizationProvider"/>
        </webParts>
-->
        
<!-- 
            Set compilation debug="true" to insert debugging symbols into the compiled page.
            Because this affects performance, set this value to true only during development.
        
-->
        
<compilation debug="true">
            
<expressionBuilders>
                
<add expressionPrefix="Version" type="VersionExpressionBuilder"/>
            
</expressionBuilders>
        
</compilation>
        
<!--
            The <authentication> section enables configuration of the security authentication
            mode used by ASP.NET to identify an incoming user. 
        
-->
        
<authentication mode="Windows"/>
        
<!--
            The <customErrors> section enables configuration of what to do if/when an unhandled
            error occurs during the execution of a request.  Specifically, it enables developers
            to configure html error pages to be displayed in place of a error stack trace.
        
-->
        
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            
<!-- 
            <error statusCode="403" redirect="NoAccess.htm"/>
            <error statusCode="404" redirect="FileNotFound.htm"/>
        
-->
        
</customErrors>
    
</system.web>
</configuration>



使用配置(profile)
//increment the current user's post cout
profile.posts = profile.posts+1
// update the current user's last post date
profile.lastpost = datetime.now;
个性化数据将存入数据库

介绍
Microsoft ASP.NET 2.0
支持被称为Profile的新对象,它可以自动在多个Web应用程序的访问之间存储用户信息。一个User Profile中可以存储各种类型的信息,这些信息既可以是简单的stringinteger类型,也可以是复杂的自定义类型。例如,你可以存储用户的姓、购物篮、用户属性或网站使用情况统计。
本文中,你将学习如何在一个应用中定义user profile。我们也会向你演示如何配置使用不同providerprofile。最后,你将学习如何管理和生成user profile的报告。

User Profiles总揽
Profile
对象与Session对象十分相似,但是更好用一些。与Session相似的地方在于,Profile是相对于一个特定的用户的,也就是说,每个Web应用程序的用户都有他们自己的profile对象。与Session不同的是,Profile对象是持久对象。如果你向Session中添加一个项,在你离开网站时,该项就会消失。而Profile则完全不同,当你修改Profile的状态时,修改在多个访问之间均有效。

profile使用provider模式来存储信息,默认情况下,user profile的内容会保存在SQL Server Express数据库中,该数据库位于网站的App_Data目录。然而,在本文的后半部分,你将了解如何使用其他数据提供者(data provider)来存储信息,如完整版的SQL Server中的一个数据库或者一个Oracle数据库。

Session不同,Profile是强类型的,Session对象仅仅是一个项集合而已,而profile对象则有强类型属性。
使用强类型是有它的道理的。例如,使用强类型,你就可以在Microsoft Visual Web Developer中使用智能感知技术,当你键入Profile和一个点的时候,智能感知会弹出你已经定义过的profile属性列表。

 

定义user profile
你既可以在machine.config中,也可以在web.config中定义一个user profile,由于你不能在应用程序的二级目录中创建一个包含文件profile节的web.config文件,这意味着你将无法在一个应用程序中定义两个以上的profile
在列表1web.config中,列举了一个简单的profile定义的实例,该profile有三个属性,FirstName, LastNamePageVisits

列表1

<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属性类型是System.String类型。列表1中,由于没有为FirstNameLastName这两个profile属性增加type特性,那么系统默认它们是string类型,而PageVisits属性则指定了type特性为Int32,因此该profile属性可用于表示一个整型值。

    最后,注意FirstNameLastName属性都有defaultValue特性。你可以为简单的数据类型设置defaultValue特性,但你不能为复杂类型设置defaultValue特性。
 
   
当你定义好一个profile之后,系统会自动在下一次页面被调用时,生成一个与该profile相对应的类。这个类会被保存在"Temporary ASP.NET Files Directory"目录(该目录也用于存放用于动态生成页面的类)。你可以使用HttpContextProfile属性(Property)调用该类。
   
当你定义好一个profile后,你可以使用如下方法为profile属性赋值。

[Visual Basic .NET]
Profile.FirstName 
= "Bill"

[C#]
Profile.FirstName 
= "Bill";

任何在web.config中定义的profile属性都会在Profile对象中呈现。
列表2演示了你该如何使用profile来持久化保存用户信息。这个页显示了FirstName,LastName, PageVisits三个属性的值,同时它包含了一个能够用于修改这三个属性的表单(form)。在Page_Load中更新PageVisits的值,这意味着每一次刷新页面,PageVisits的值都会改变。

列表 2. Simple.aspx (C#)

<%@ 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>

如果你多次访问列表2中的页面,你会注意到PageVisits在不断增大。如果你关闭的浏览器,并在一周之后调用该页面,PageVisits属性仍然会保留原值。从这一点可以看出Profile为每个用户自动保存一个副本。
posted on 2008-02-28 15:39  北漂浪子  阅读(268)  评论(0编辑  收藏  举报