- 你所在位置:首页 〉VS.net〉ASP.net〉开发〉ASP.NET 2.0下实现匿名用户向注册用户的迁移(上)
- ASP.NET 2.0下实现匿名用户向注册用户的迁移(上)
-
作者:巡山小牛 文章来源:www.cnblogs.com 发布日期:2008-11-01 浏览次数:63
-
- 打印这篇文章
-
演示环境:Win2003 + Visual Studio 2005 + Sql Server2005,前期需要将MS在Sql2000下的例程数据库Northwind加入Sql Server 2005;注意:操作系统不一定非要Win2003,凡是支持.net FrameWork 2.0的都可
新建一个例程网站,该网站目前结构如下图:

这是一个还未使用任何用户管理技术的网站,那么我们下面就从头开始讨论这些技术.
前期页面准备工作如下:
Default.aspx前后台代码如下:

Code

〈%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

〈!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
〈html xmlns="http://www.w3.org/1999/xhtml">
〈head id="Head1" runat="server">
〈title>示例13-3〈/title>
〈link id="InstanceStyle" href="StyleSheet.css" type="text/css" rel="stylesheet">
〈/head>
〈body>
〈form id="form1" runat="server">
〈div>
〈fieldset style="width: 620px">
〈legend class="mainTitle">实现匿名用户向注册用户迁移〈/legend>
〈table border="0" cellpadding="5" width="620px">
〈tr>
〈td style="font-size:small;">当前用户是:〈asp:Label ID="lbUserName" runat="server">〈/asp:Label>
〈/td>
〈td>
〈asp:LinkButton ID="lbtLogin" runat="server" CssClass="littleTitle" OnClick="lbtLogin_Click">登录〈/asp:LinkButton>
〈asp:LinkButton ID="lbtLogout" runat="server" CssClass="littleTitle" OnClick="lbtLogout_Click">退出〈/asp:LinkButton>〈/td>
〈/tr>
〈tr>
〈td colspan="2">〈hr />
〈/td>
〈/tr>
〈tr align="center">
〈td>
〈p class="littleTitle">
待售商品列表〈/p>
〈/td>
〈td>
〈p class="littleTitle">
购物车〈/p>
〈/td>
〈/tr>
〈tr>
〈td>
〈asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="〈%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">〈/asp:SqlDataSource>
〈asp:GridView ID="ProductGrid" runat="server"
Width="300px" AllowPaging="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1" Font-Size="Small" PageSize="5">
〈FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
〈RowStyle BackColor="White" ForeColor="#330099" />
〈Columns>
〈asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/button_buy.gif" ShowSelectButton="True" />
〈asp:BoundField DataField="ProductName" HeaderText="商品名称" SortExpression="ProductName" />
〈asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="单价" SortExpression="UnitPrice" />
〈/Columns>
〈PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
〈SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
〈HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
〈/asp:GridView>
〈/td>
〈td valign="top">
〈asp:GridView ID="CartGrid" AutoGenerateColumns="False" DataKeyNames="ID"
CellPadding="4" Width="320px" runat="Server" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" Font-Size="Small">
〈Columns>
〈asp:ButtonField CommandName="select" Text="Remove" ButtonType="Image" ImageUrl="~/Images/button_del.gif" />
〈asp:BoundField DataField="Name" HeaderText="商品名称" />
〈asp:BoundField DataField="Price" HeaderText="单价" DataFormatString="{0:c}" />
〈asp:BoundField DataField="Quantity" HeaderText="数量" />
〈/Columns>
〈FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
〈RowStyle BackColor="White" ForeColor="#330099" />
〈PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
〈SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
〈HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
〈/asp:GridView>
〈asp:Label ID="lblTotal" runat="Server" CssClass="littleTitle" />〈/td>
〈/tr>
〈/table>
〈/fieldset>
〈/div>
〈/form>
〈/body>
〈/html>



Default.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
11
public partial class _Default : System.Web.UI.Page
12

{
13
protected void Page_Load(object sender, EventArgs e)
14
{
15
16
}
17
protected void lbtLogin_Click(object sender, EventArgs e)
18
{
19
Response.Redirect("Login.aspx");
20
}
21
protected void lbtLogout_Click(object sender, EventArgs e)
22
{
23
FormsAuthentication.SignOut();
24
}
25
}
26
Login页面的前后代码如下:

Login.aspx
〈%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
〈!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
〈html xmlns="http://www.w3.org/1999/xhtml">
〈head runat="server">
〈title>示例13-3〈/title>
〈link id="InstanceStyle" href="StyleSheet.css" type="text/css" rel="stylesheet">
〈/head>
〈body>
〈form id="form1" runat="server">
〈div>
〈fieldset style="width: 260px">
〈legend class="mainTitle">站点登录〈/legend>
〈br />
〈p style="font-size: small">提示:用户名是Tom,密码是Tom@Tom.com〈/p>
〈asp:Login ID="Login1" runat="server" Font-Size="Small" LoginButtonText="提交" PasswordLabelText="密码:" TitleText="" UserNameLabelText="用户名:" FailureText="输入不正确." RememberMeText="下次自动登录">
〈TextBoxStyle Width="150px" />
〈TitleTextStyle Font-Bold="True" />
〈CheckBoxStyle HorizontalAlign="Right" />
〈/asp:Login>
〈/fieldset>
〈/div>
〈/form>
〈/body>
〈/html>
Login页面没有后台代码,该页面需要用到一个成员用户管理类别的控件:Login1,请注意。
以上就是页面上主要两个页面的设计情况,再就是些样式设计StyleSheet.css和Web.config如下:

StyleSheet.css
1
body
2
{
}{
3
}
4
.mainTitle
5
{
}{
6
font-size: 12pt;
7
font-weight: bold;
8
font-family: 宋体;
9
}
10
.commonText
11
{
}{
12
font-size: 11pt;
13
font-family: 宋体;
14
}
15
.littleTitle
16
{
}{
17
font-size: 10pt;
18
font-family: 宋体;
19
font-weight:bold;
20
}
21

Web.Config
1
〈?xml version="1.0"?>
2
3
〈configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
4
〈appSettings/>
5
〈connectionStrings>
6
〈add name="NorthwindConnectionString" connectionString="Data Source=ROGER;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
7
〈/connectionStrings>
8
〈system.web>
9
10
〈authentication mode="Forms">
11
〈forms loginUrl="Login.aspx">〈/forms>
12
〈/authentication>
13
14
〈compilation debug="true"/>
15
〈/system.web>
16
〈/configuration>
注意:以上数据库连接字符串需要自己配置,这里配置是Windows登录,而且Data Source根据每台机器自己配置。
以上由Web.Config可以看出,此时页面验证方式是Forms。
此时运行Default.aspx页面是可以出现下图左边购物车的数据,该数据来自Northwind数据库,因为界面上通过SqlDataSource控件进行数据沟通的,如下两个图


此时在登录界面(Login.aspx)进行登录是无法实现的,因为此时未引入Profile概念,那么此概念将在下面介绍。
ASP.NET 2.0在处理用户配置信息方面基本抛弃了ASP.NET 1.X的做法,其提供的个性化用户配置功能可以实现将用户配置信息与单个用户关联,并采取持久化方式存储信息。理解这个概念需要注意以下四点:一是配置信息可以是与任何用户有关的信息。二是所存储的信息可以是可以是任何数据类型的对象,比如甚至可以是复杂的自定义数据类型。三是单个用户可以是注册用户,也可以是匿名用户。四是默认情况下,持久存储采用SQL Server数据库方式,并且无需自行创建或维护数据库,这些工作都有ASP.NET 2.0自动完成。
使用个性化用户配置功能主要包括如下两个核心的步骤:首先,配置应用程序以便启用和定义要为用户存储和跟踪的配置信息。这些工作在Web.config文件的〈Profile>中轻松完成。然后,使用与用户配置功能有关的强类型API实现对用户配置信息的存储、访问和管理等。
那么,下面先来配置Web.config文件,加入如下代码:

加入Profile配置节的Web.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
注意:上述红色部分代码(10~15行)是新加入的配置节
10行:标注是否需要对匿名用户进行身份记录
Profile节包括〈properties>和〈providers>两大子配置节,我们先看〈properties>,13行此处是我们准备存储的ShoppingCart类,这个类用来记录购物车信息,type="ShoppingCart" 就是指向待会建立的类名叫ShoppingCart,后面二个属性表明这个类是可序列化为二进制,并且运行匿名用户访问的。
下面在App_Code目录下建立ShoppingCart类,代码如下:

ShoppingCart.cs
1
using System;
2
using System.Collections;
3
4
[Serializable]
5
public class ShoppingCart
6

{
7
public Hashtable _CartItems = new Hashtable();
8
// 创建属性CartItems,用于获取购物车中所有商品
9
public ICollection CartItems
10
{
11
get
{ return _CartItems.Values; }
12
}
13
14
// 创建属性Total,用于获取购物车中商品总价
15
public decimal Total
16
{
17
get
18
{
19