Profile 详解之匿名状态迁移至登录状态

来源:互联网 发布:linux下安装psutil 编辑:程序博客网 时间:2024/05/17 04:30

2010-03-02 15:26 by ╭☆涩 轨ら, 127 visits, 网摘,收藏,编辑

首先还是来解释一下博文的标题意义吧,

何为匿名状态,何为登录状态,又何为匿名状态迁移至登录状态?

匿名状态就是没有登录这个网站而对网站进行访问的状态,

而登录状态则是用户成功登录了网站,

而从匿名状态迁移至登录状态就是一个用户一开始并没有登录,

而是以匿名的身份证登录这个网站,而后浏览了一会儿又想登陆来发表篇博文,

这就是从匿名状态迁移至登录状态,

貌似这里没有什么好介绍的呢?

不过,再请考虑一个问题,就是比如说用户去购物吧,

用户购物一般都是在购物网站上先以匿名身份选择要买的商品,

而后等选好了以后,用户便进行登陆,然后付款等等后续工作,

但是,这其中会有一个问题,就是用户以匿名身份选择的商品怎么迁移至登录状态呢?

也就是以匿名身份选择的商品,登录后还要保存这些商品来进行付款!!!

先来看一个单词吧----Migration,就是迁移的意思,大伙先记住这个单词,

等下后面就会知道其用处了,

下面在介绍 Demo 之前呢,还是先讲一下一个文件 Global.asax ,也就是全局应用程序类,

您可以在这个文件中使用 Profile_MigrateAnonymous 全局事件

来访问 ProfileModule 类的 MigrateAnonymous 事件,

而当匿名使用应用程序的用户进行登陆时,就会触发 Profile_MirateAnonymous 事件,

所以就可以使用 MigrateAnonymous 事件将配置文件 Profile 的属性值

从匿名 Profile 复制到已验证身份的用户的配置文件中去

这样便完成了匿名状态到登录状态信息的一个完整的迁移。

下面就来看一个 Demo 吧

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

<script runat="server">

    protected void btnSava_Click(object sender, EventArgs e)
    {
        if (Profile.IsAnonymous)
        {
            //先将状态保存到匿名用户的 Profile 中
            Profile.照片 = "~/Photo/" + ddlImageName.SelectedValue;
            Profile.Save();

            lblSaveMsg.Text = "恭喜您,保存成功!!!";
        }
        else
        {
            lblSaveMsg.Text = "你目前已不是匿名状态,所以不能保存!!!";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //将 Photo 目录下的所有的 .jpg 文件绑定到 DropDownList 上面
            ddlImageName.Items.Clear();
            System.IO.DirectoryInfo dirInfo =
                new System.IO.DirectoryInfo(Server.MapPath("~/Photo/"));
            foreach (System.IO.FileInfo file in dirInfo.GetFiles())
            {
                if (file.Extension.ToLower() == ".bmp")
                {
                    ddlImageName.Items.Add(
                        new ListItem(file.Name, file.Name));
                }
            }
        }
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        if (!Profile.IsAnonymous)
        {
           
//获取登录成功后的登录用户的 ProfileCommon 实例
            ProfileCommon currentProfile =
               
Profile.GetProfile(Profile.UserName);
            lblName.Text = currentProfile.UserName;
            lblPhone.Text = currentProfile.电话.家庭电话 + "   " +
                currentProfile.电话.移动电话;
            lblPhoto.Text = currentProfile.照片;
            lblConstellate.Text = currentProfile.星座;
            lblAddress.Text = currentProfile.住址;

        }
        else
        {
            lblSaveMsg.Text = "请先登录!!!";
            return;
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 30%;
            border: 1px solid #8000FF;
            height: 210px;
        }
        .style2
        {
            height: 25px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: 微软雅黑; font-size: small">
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br />
        <br />
        选择头像&nbsp;&nbsp;
        <asp:DropDownList ID="ddlImageName" runat="server" Width="150px" >
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="lblSaveMsg" runat="server" Text=""></asp:Label>
        <br />
        <br />
        <asp:Button ID="btnSava" runat="server" OnClick="btnSava_Click"
        Text="匿 名 保 存 Profile" />
        &nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnQuery" runat="server" Text="获 取 用 户 Profile"
            onclick="btnQuery_Click" />
        <br />
        <table class="style1">
            <tr>
                <td>
                    用户名</td>
                <td>
                    <asp:Label ID="lblName" runat="server" Text="">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    住址</td>
                <td>
                    <asp:Label ID="lblAddress" runat="server" Text="">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    电话</td>
                <td>
                    <asp:Label ID="lblPhone" runat="server" Text="">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    星座</td>
                <td>
                    <asp:Label ID="lblConstellate" runat="server" Text="">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    照片</td>
                <td>
                    <asp:Label ID="lblPhoto" runat="server" Text="">
                    </asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    </td>
                <td class="style2">
                    </td>
            </tr>
        </table>
        <br />
    </div>
    </form>
</body>
</html>

然后还得开一下 Global.asax 中的 Profile_MigrateAnonymous 事件的定义

在这个事件中呢,完成了将信息从匿名用户的 Profile 中转存到了登录状态的 Profile 中,

同时也删除了这个匿名用户在数据表 aspnet_Profile 和 aspnet_Users 中的数据,

并且也清除了为这个匿名用户在客户端生成的 Cookie 文件

image

下面就来看演示了

首先,直接以匿名方式浏览这个 Demo

image

然后您打开您机器上存储 Cookie 的地方,您可以看到新建了一个 Cookie 文件

image

这个 Cookie 便是 ASP.NET 为此次浏览的匿名用户分配的 GUID 存储的位置,

然后您选择一个头像进行存储

image

此时再来看数据表 aspnet_Users

image

这里就可以看到在数据表中新增了一个匿名用户

然后再来看数据表 aspnet_Profiles

image

这里就可以知道刚刚添加的信息时添加给了匿名用户的

接下来就要进行登陆了

(以 ChengYan 进行登陆,

在这个登陆过程中会触发在 Global.asax 中定义的 Profile_MigrateAnonymous 事件,

所以一开始的匿名用户信息会在 aspnet_Uses 表和 aspnet_Profile 表中均删除

同时也会删除这个匿名用户在客户端的 Cookie)

image

登陆成功后,您再去看您客户机 Cookie 所在的位置,

刚才上面截图的那个 xiaozhen@localhost[1].txt 的 Cookie 文件便被删除了,

然后再来看数据表 aspnet_Users ,您可以发现其中刚才的那个匿名用户也被删除了,

再来看数据表 aspnet_Profile ,您会发现登录前的那个匿名用户的 Profile 也被删除了,

image

而发现有一个却增加了属性,也就是 ChengYan 这个用户,

其本来没有照片这个 Profile 属性的,当是登陆成功后,由于将匿名信息迁移了过来,

所以其又有了照片这个 Profile 属性

image

经过了上面的演示,我想大家也完整的看到了从匿名状态迁移到登录状态的全过程,

并且其中的一些要点,我也说得比较详细了,这篇博文的目的也就达到了!!!