首页  ·  知识 ·  云计算
使用ASP.NET2.0Profile存储用户信息(2)
佚名  本站原创    编辑:dezai  图片来源:网络
列表5 ShoppingCart (c#) using System;using System.Collections; [Serializable]public class Sh

 

 

列表5 ShoppingCart (c#)

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目录中,购物篮会自动被编译。

 


在列表5中有一点值得注意,那就是ShoppingCart和CartItem类都加上了可序列化的特性,这一点对于他们能否被序列化十分重要,只有这样才能保存在Profile对象中。


最后,列表6的页面显示了可以被添加到购物篮中的产品。购物篮是通过BindShoppingCart方法从Profile对象中载入,该方法把购物篮中的对象绑定到一个GridView对象上,这些对象可以通过ShoppingCart类的CartItems属性获得。

 

图2 在profile中存储购物篮


AddCartItem方法用于在购物篮中添加一个产品,该方法中包含了检测Profile是否存在ShoppingCart的代码。对于Profile中存储的对象,你必须自己实例化这些对象,他们不会自动实例化。


RemoveCartItem方法用于从购物篮中移除一个产品,该方法只是简单地通过调用Profile中的ShoppingCart对象的RemoveItem方法。

 


列表 6 - Products.aspx (Visual Basic .NET)

<%@ Page Language="VB" %>



    Products


   

   


   
       
       
    
    

   

Products

   
            ID="ProductGrid"
        DataSourceID="ProductSource"
        DataKeyNames="ProductID"
        AutoGenerateColumns="false"
        OnSelectedIndexChanged="AddCartItem"
        ShowHeader="false"
        CellPadding="5"
        Runat="Server">
       
                            CommandName="select"
                Text="Buy" />
                            DataField="ProductName" />
                            DataField="UnitPrice"
                DataFormatString="{0:c}" />
       

   

 

       
            ID="ProductSource"
        ConnectionString=
"Server=localhost;Database=Northwind;Trusted_Connection=true;"
        SelectCommand=
          "SELECT ProductID,ProductName,UnitPrice FROM Products"
        Runat="Server" />
       


       

Shopping Cart


                    ID="CartGrid"
            AutoGenerateColumns="false"
            DataKeyNames="ID"
            OnSelectedIndexChanged="RemoveCartItem"
            CellPadding="5"
            Width="300"
            Runat="Server">
           
                            CommandName="select"
                Text="Remove" />
                            DataField="Name"
                HeaderText="Name" />
                            DataField="Price"
                HeaderText="Price"
                DataFormatString="{0:c}" />
                            DataField="Quantity"
                HeaderText="Quantity" />
           

       
        Total:
       
       

   

 


列表 6. Products.aspx (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>



    Products


   

   


   
       
       
    
    

   

Products

   
            ID="ProductGrid"
        DataSourceID="ProductSource"
        DataKeyNames="ProductID"
        AutoGenerateColumns="false"
        OnSelectedIndexChanged="AddCartItem"
        ShowHeader="false"
        CellPadding="5"
        Runat="Server">
       
                            CommandName="select"
                Text="Buy" />
                            DataField="ProductName" />
                            DataField="UnitPrice"
                DataFormatString="{0:c}" />
       

   

 

       
            ID="ProductSource"
        ConnectionString=
"Server=localhost;Database=Northwind;Trusted_Connection=true;"
        SelectCommand=
          "SELECT ProductID,ProductName,UnitPrice FROM Products"
        Runat="Server" />
       


       

Shopping Cart


                    ID="CartGrid"
            AutoGenerateColumns="false"
            DataKeyNames="ID"
            OnSelectedIndexChanged="RemoveCartItem"
            CellPadding="5"
            Width="300"
            Runat="Server">
           
                            CommandName="select"
                Text="Remove" />
                            DataField="Name"
                HeaderText="Name" />
                            DataField="Price"
                HeaderText="Price"
                DataFormatString="{0:c}" />
                            DataField="Quantity"
                HeaderText="Quantity" />
           

       
        Total:
       
       

   



继承一个profile
你也可以通过从一个已经存在的profile类中继承一个profile来完成对profile的定义,这种特性能够帮助你在多个应用程序中使用相同的profile。
例如,列表7中列出了一个拥有多个用户属性的类,该类是从ProfileBase类继承而来的(你可以在System.Web.Profile中找到)

 


在列表8中的Web.config包含一个从UserInfo类继承而来的profile,通过该声明,新的profile可以获得UserInfo类的所有属性。


列表 7. UserInfo (Visual Basic .NET)

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
列表 7. UserInfo (C#)
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; }
    }
}
列表 8. Web.Config


   
          
  
   

迁移匿名Profile设置
Profile对象既可用于匿名用户也可以用于已认证用户。然而,当用户从匿名用户状态转换为已认证用户状态时,Profile对象能够以一种令人难以理解的方式完成任务。
当匿名用户使用Profile对象时,用户profile是与一个随机生成的号码相关联的,该号码是根据每个用户唯一生成的,它保存在浏览器的cookie中,无论何时该用户返回应用程序,该用户的Profile设置会被自动加载。
如果匿名用户通过认证的话,所有与该用户相关的profile就会丢失,同时系统会生成一个新的profile。这时该Profile信息将与用户名相关联,而非唯一识别号。
要想理解所有这些工作,最好的方法就是看看下面的例子。列表9中的web.config定义了一个profile,该profile只有一个FavoriteColor属性。

列表 9 Web.config


  
     
          
       
  
  
                           name="FavoriteColor"
         allowAnonymous="true"
         defaultValue="Red" />
              

          



列表10中有一个包含两个按钮的页面,分别是login和logout按钮,其中还有一个用于更新FavoriteColor属性的表单。
列表10. Anonymous.aspx (Visual Basic .NET)

<%@ Page Language="VB" %>



    Anonymous


   

            Text="Login"
        onClick="Login"
        Runat="Server" />
            Text="Logout"
        onClick="Logout"
        Runat="Server" />
   



            id="txtFavoriteColor"
        Runat="Server" />
            Text="Update Profile"
        onClick="UpdateProfile"
        Runat="Server" />
   

    Username:
            id="lblUsername"
        Runat="Server" />
   

    Favorite Color:
            id="lblFavoriteColor"
        Runat="Server" />   
       
   


列表10. Anonymous.aspx (C#)

<%@ Page Language="C#" %>



    Anonymous


   

            Text="Login"
        onClick="Login"
        Runat="Server" />
            Text="Logout"
        onClick="Logout"
        Runat="Server" />
   



            id="txtFavoriteColor"
        Runat="Server" />
            Text="Update Profile"
        onClick="UpdateProfile"
        Runat="Server" />
   

    Username:
            id="lblUsername"
        Runat="Server" />
   

    Favorite Color:
            id="lblFavoriteColor"
        Runat="Server" />   
       
   


当你打开第一个页面时,UserName的值是一个随机生成的唯一识别号(见图3)。当你按下Login按钮后,你就完成了身份认证,它是通过用户票据(User Bill)完成的。


图3 使用匿名和认证profile

列表10的页面中包含一个用于更新FavoriteColor的表单,要注意的是,在你登录登出的时候,会分别生成两个不同的profile。例如当你先登录,后登出的话,那么系统会生成一个随机的唯一识别号。

在很多情况下,你需要把匿名profile迁移到认证profile状态,如果你需要迁移profile属性值的话,你可以利用 ProfileModule类的MigrateAnonymous事件完成该任务,该事件只能在Global.asax文件中进行处理。列表11中的 Global.asax演示了你如何才能实现FavoriteColor属性的迁移。

列表 11. Global.asax (Visual Basic .NET)

<%@ Application Language="VB" %>

列表 11. Global.asax (C#)

<%@ Application Language="C#" %>

通过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列出了该配置文件。

列表 12. Web.Config



name="myConnectionString"
connectionString=
"Server=MyServer;Trusted_Connection=true;database=MyDatabase" />





name="MyProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="myConnectionString" />


name="FirstName"
allowAnonymous="true" />
name="LastName"
allowAnonymous="true" />




在列表12中的profile配置中,包含了一个defaultProvider特性,这个特性指向一个叫MyProfileProvider的 profile provider,而这个provider定义是在profile标记的节中完成的。 MyProfileProvider则使用一个叫MyConnectionString的连接字符串完成数据库连接,并保存profile信息到数据库中。MyConnectionString可以在位于web.config开头的节中找到。

管理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总数
这些方法中,虽然所有的方法都返回一个ProfileInfo对象集合,但没有一个返回一个真正的profile。ProfileInfo对象包含以下profile属性

IsAnonymous. 表示该profile是否为匿名profile

LastActivityDate. 最后一次profile被访问的时间和日期
LastUpdatedDate. 最后一次profile被升级的时间和日期
Size. 表示profile的大小,这是在profile provider存储profile信息时记录的
UserName. 与profile关联的用户名
ProfileManager有几个方法提供了额外的参数用于支持分页。例如,GetAllProfiles方法的一个重载版本就提供了专门用于设置页面索引、页面大小、总共的记录数的参数,这些参数在需要分页的页面中十分有用。

ProfileManager 既可以在asp.net页面下使用,也可以在其它程序中使用。例如,你可能需要做一个控制台程序用于每天清除长时间未使用的 profile。列表14的控制台程序会删除七天未使用的profile,你可以使用Windows计划任务(Windows Scheduled Tasks)来安排该程序的执行时间。

列表 14. DeleteInactiveProfiles (Visual Basic .NET)

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
列表 14. DeleteInactiveProfiles (C#)

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进行编译

[Visual Basic .NET]
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

Listing 15. Web.Config

http://schemas.microsoft.com/.NetConfiguration/v2.0">
   
  

     
             

     
     
                                  name="SurveyCompleted"
            type="Boolean"
            allowAnonymous="true" />
                                  name="FavoriteLanguage"
            allowAnonymous="true" />
                                 name="FavoriteEnvironment"
            allowAnonymous="true" />
     

             

 

列表16中的页面演示了一个简单的用户调查。该页面包含两个Panel控件,第一个控件中有两个调查问题,当用户完成调查后,第一个控件会自动隐藏,而第二个会显示出来,第二个Panel有一段表示感谢的文字。

列表 16. Survey.aspx (Visual Basic .NET)

<%@ Page Language="VB" %>



    Survey


   


   
   
    What is your favorite programming language?
   

            id="rdlLanguage"
        runat="Server">
           
       
           
   

   

 


    What is your favorite development environment?
   

            id="rdlEnvironment"
        runat="Server">
           
       
           
   

   

 

   
            Text="Submit Survey"
        onclick="SaveSurvey"
        Runat="Server" />
   

   
    Thank you for completing the survey!
   

   



列表 16. Survey.aspx (C#)

<%@ Page Language="C#" %>



    Survey


   


   
   
    What is your favorite programming language?
   

            id="rdlLanguage"
        runat="Server">
           
       
           
   

   

 


    What is your favorite development environment?
   

            id="rdlEnvironment"
        runat="Server">
           
       
           
   

   

 

   
            Text="Submit Survey"
        onclick="SaveSurvey"
        Runat="Server" />
   

   
    Thank you for completing the survey!
   

   



列表17中显示调查的结果,该页面中有一个显示ProfileInfo对象集合的GridView控件,该ProfileInfo对象集合是由 ProfileManager的GetAllProfiles方法获得的。当你点击GridView中的任意一行的Select链接时,你将会看到对这个问题的调查结果,该调查结果是由Profile类的GetProfile方法获得的。


图5 显示调查结果

列表 17. SurveyResults.aspx (Visual Basic .NET)

<%@ Page Language="VB" %>



    Survey Results


   


   

Survey Results


            id="ResultsGrid"
        DataKeyNames="UserName"
        AutoGenerateSelectButton="true"
        OnSelectedIndexChanged="DisplayProfileDetails"
        SelectedRowStyle-BackColor="LightYellow"
        Runat="Server" />
   

 


   

Survey Details


    Favorite Language:
            id="lblLanguage"
        Runat="Server" />
   

    Favorite Environment:
            id="lblEnvironment"
        Runat="Server" />

   




列表 17. SurveyResults.aspx (C#)

<%@ Page Language="C#" %>



    Survey Results


   


   

Survey Results


            id="ResultsGrid"
        DataKeyNames="UserName"
        AutoGenerateSelectButton="true"
        OnSelectedIndexChanged="DisplayProfileDetails"
        SelectedRowStyle-BackColor="LightYellow"
        Runat="Server" />
   

 


   

Survey Details


    Favorite Language:
            id="lblLanguage"
        Runat="Server" />
   

    Favorite Environment:
            id="lblEnvironment"
        Runat="Server" />
   

总结
    当建立Web应用程序时,我依旧花费了大量的时间和精力用于做一些大伤脑筋的事情。其中的一个任务就是写一些用于从数据库存储和获得用户信息的代码。虽然 Profile对象引入的都是asp.net 1.0中可以实现的功能,但是这个新特性帮助我们从乏味的编码工作中解脱出来,这样也能让我们在写Web应用程序的过程中,把更多的精力放在我们更感兴趣的事情上。

本文作者:佚名 来源:本站原创
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的