个性户用户配置
个性化功能简介
大多数用户以匿名形式访问Web站点时,希望实现诸多内容定制、布局调用等功能。
ASP.NET 2.0 技术提供了一个个性化服务解决技术框架。该框架主要包括3项核心功能:个性化用户配置、Web部件、成员和角色管理
个性化服务分为三大步骤:
(1)识别用户身份
(2)提供个性化服务体验
(3)储存用户信息
<Profile>配置节
设置<profile>配置节,经常对其三部分进行设置:
- <profile>自身属性
- 子节<properties>属性设置
- 子节<providers>属性设置
个性化用户配置API
1.创建Login.aspx页面,做为用户登录页面
<body>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Welcome.aspx">
</asp:Login>
</div>
</form>
</body>
2.创建Welcome.aspx页面
<body>
<form id="form1" runat="server">
<div>
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
欢迎:<asp:LoginName ID="LoginName1" runat="server" />
<br />
<asp:HyperLink ID="hlInfo" runat="server" NavigateUrl="~/ProfileInfo.aspx">
添加简单的个性化信息
</asp:HyperLink>
</LoggedInTemplate>
</asp:LoginView>
</div>
</form>
</body>
protected void Page_Load(object sender, EventArgs e)
{
//判断
if (!IsPostBack)
{
//已经登录的用户
if (Profile.IsAnonymous == false)
{
Response.Write(Profile.strName + "<br/>" + Profile.strPhone + "<br/>" + Profile.birthDate.ToShortDateString() + "<br/>");
foreach (string strName in Profile.BookList)
{
Response.Write("书籍名称:" + strName + "<br/>");
}
}
}
}
3.添加ProfileInfo.aspx页面,处理用户个性化设置
protected void save_Click(object sender, EventArgs e)
{
if (Profile.IsAnonymous == false)
{
//获取前台的值
Profile.strName = this.txtName.Text;
Profile.strPhone = this.Phone.Text;
//格式转化(1900-09-09)
Profile.birthDate = Convert.ToDateTime(this.birthDate.Text);
//CheckBoxList(空)
Profile.BookList = new System.Collections.Specialized.StringCollection();
//CheckBoxList所有数据
ListItemCollection listItem = this.CheckBoxList1.Items;
//判断listItem哪些是被选择的
foreach (ListItem i in listItem)
{
//是否被选中
if (i.Selected)
{
//添加到自定义数据集合里
Profile.BookList.Add(i.Text);
}
}
Response.Redirect("Welcome.aspx");
}
}
使用个性化配置存储复杂的类型
在开发大型应用程序和商业站点的时候,经常不得不存储复杂的用户自定义数据类型和集合
<properties>
<add name="strName" type="System.String" allowAnonymous="true"/>
<add name="strPhone" allowAnonymous="true"/>
<add name="birthDate" type="System.DateTime" allowAnonymous="true"/>
<!--自定义的数据集合-->
<add name="BookList" type="System.Collections.Specialized.StringCollection" allowAnonymous="true"/>
</properties>
</profile>
protected void save_Click(object sender, EventArgs e)
{
if (Profile.IsAnonymous == false)
{
//获取前台的值
Profile.strName = this.txtName.Text;
Profile.strPhone = this.Phone.Text;
Profile.birthDate = Convert.ToDateTime(this.birthDate.Text);
Profile.BookList = new System.Collections.Specialized.StringCollection();
//CheckBoxList所有数据
ListItemCollection listItem = this.CheckBoxList1.Items;
//判断listItem哪些是被选择的
foreach (ListItem i in listItem)
{
//是否被选中
if (i.Selected)
{
//添加到自定义数据集合里
Profile.BookList.Add(i.Text);
}
}
Response.Redirect("Welcome.aspx");
}
else
{
//CheckBoxList(空)
Profile.BookList = new System.Collections.Specialized.StringCollection();
//CheckBoxList所有数据
ListItemCollection listItem = this.CheckBoxList1.Items;
//判断listItem哪些是被选择的
foreach (ListItem item in listItem)
{
//是否被选中
if (item.Selected)
{
//添加到自定义数据集合里
Profile.BookList.Add(item.Text);
}
}
Response.Redirect("loginnone.aspx");
}
}
修改Welcom.aspx页面
protected void Page_Load(object sender, EventArgs e)
{
//判断
if (!IsPostBack)
{
//判断是否是已经登录的用户
if (Profile.IsAnonymous == false)
{//打印用户个性化的信息
Response.Write(Profile.strName + "<br/>" + Profile.strPhone + "<br/>" +
Profile.birthDate.ToShortDateString() + "<br/>");
Response.Write("书名:<br>");
//打印书名
foreach (string strName in Profile.BookList)
{
Response.Write("书籍名称:" + strName + "<br/>");
}
}
}
}
匿名个性化配置
开启用户匿名个性化