ASP.NET2.0个性化用户配置之存储匿名/登录用户信息

个性化用户配置是ASP.NET2.0的一个新特性,它主要依赖于ProfileBase类和ProfileManager类,其中ProfileBase类提供对配置文件属性值和信息的非类型化访问,ProfileManager类管理用户配置文件数据和设置。在启动启用了用户配置文件的应用程序时,ASP.NET会创建一个类型为ProfileCommon的新类,该类从ProfileBase类继承。强类型访问器被添加到profile配置节中为每个属性定义的ProfileCommon类中。ProfileCommon类的强类型访问器调用ProfileBase基类的GetPropertyValue和SetPropertyValue方法,分别用于配置文件属性值的检索和设置。ProfileCommon类的一个实例被设置为ASP.NET应用程序的Profile属性的值。

使用Profile与使用Session十分相似,但是更好用一些。与Session相似的地方在于,Profile是相对于一个特定的用户的,也就是说,每个Web应用程序的用户都有他们自己的profile对象。与Session不同的是,Profile对象是持久对象,因为他默认把信息保存在SQL Server Express数据库中,如果你向Session中添加一个项,在你离开网站时,该项就会消失,而Profile则完全不同,当你修改Profile的状态时,修改在多个访问之间均有效。还有另一点与Session不同,Profile是强类型的,Session对象仅仅是一个项集合而已,而profile对象则有强类型属性。

下边例子演示了使用个性化用户配置来保存匿名和登录用户信息:

web.config:
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="MySQL" connectionString="uid=sa;password=sa;database=aspnetdb;server=127.0.0.1" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    
    <system.web>
        <authentication mode="Forms" />
        <anonymousIdentification enabled="true"/>
        <profile defaultProvider="SqlProvider">
            <properties>
                <add name="Country" type="String" allowAnonymous="true" />
                <add name="Province" type="String" allowAnonymous="true"/>
            </properties>
            
            <providers>
                <clear/>
                <add name="SqlProvider" connectionStringName="MySQL" type="System.Web.Profile.SqlProfileProvider" applicationName="MyApp" />
            </providers>
        </profile>
    </system.web>
</configuration>

default.aspx:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server" language="C#">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //显示用户(匿名/登录)信息
            Label1.Text = "<b>UserName:</b>" + Profile.UserName;
            Label1.Text += "<br/><b>Country:</b>" + Profile.Country;
            Label1.Text += "<br/><b>Province:</b>" + Profile.Province;
        }
    }

    /// <summary>
    /// 保存用户(匿名/登录)信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Save_Click(object sender, EventArgs e)
    {
        Profile.Country = TextBox1.Text;
        Profile.Province = TextBox2.Text;
        Response.Redirect(Request.Path, false);
    }

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Login_Click(object sender, EventArgs e)
    {
        string userName = "mzwu.com";
        FormsAuthentication.SetAuthCookie(userName, true);
        Response.Redirect(Request.Path, false);
    }

    /// <summary>
    /// 退出登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Logout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect(Request.Path, false);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>个性化用户配置-Mzwu.Com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Country:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        Province:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Click" />
        <asp:Button ID="Login" runat="server" Text="Login" OnClick="Login_Click" />
        <asp:Button ID="Logout" runat="server" Text="Logout" OnClick="Logout_Click" /><br /><br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

演示步骤:

1.打开页面后输入Country和Province的值,按Save按钮保存,页面上显示当前为匿名用户及其输入的值;
2.点击Login按钮进行登录,再次输入Country和Province的值按Save按钮保存,页面上显示当前用户名及输入的值;
3.点击Logout按钮退出登录,页面上显示的是匿名用户及输入的值;
4.再点击Login按钮进行登录,页面上又显示登录用户mzwu.com输入的值;
5.有兴趣的朋友可更新保存退出登录再登录试试;

评论: 0 | 引用: 0 | 查看次数: 4328
发表评论
登录后再发表评论!