web.config配置
<
anonymousIdentification
enabled
="true"
/>
< profile >
< properties >
< add name ="Name" allowAnonymous ="true" />
< add name ="LastSubmit" type ="System.DateTime" allowAnonymous ="true" />
< group name ="Address" >
< add name ="City" allowAnonymous ="true" />
< add name ="PostalCode" allowAnonymous ="true" />
</ group >
</ properties >
</ profile >
< profile >
< properties >
< add name ="Name" allowAnonymous ="true" />
< add name ="LastSubmit" type ="System.DateTime" allowAnonymous ="true" />
< group name ="Address" >
< add name ="City" allowAnonymous ="true" />
< add name ="PostalCode" allowAnonymous ="true" />
</ group >
</ properties >
</ profile >
代码:
protected
void
Page_Load(
object
sender, EventArgs e)
{
if ( ! Page.IsPostBack)
{
// 显示用户配置信息
DisplayProfileInfo();
}
}
protected void btnSubmit_Click( object sender, EventArgs e)
{
// 保存用户配置信息到Profile属性中
Profile.Name = txtName.Text;
Profile.Address.City = txtCity.Text;
Profile.Address.PostalCode = txtPostalCode.Text;
Profile.LastSubmit = DateTime.Now;
// 显示用户配置信息
DisplayProfileInfo();
}
private void DisplayProfileInfo()
{
// 从Profile属性中获取数据并赋值给服务器控件
txtName.Text = Profile.Name;
txtCity.Text = Profile.Address.City;
txtPostalCode.Text = Profile.Address.PostalCode;
DateTime time = Profile.LastSubmit;
// 如果未获取值则显示空,否则显示获取的值
if (time.Year == 1 )
{
labLastSubmit.Text = " 空 " ;
}
else
{
labLastSubmit.Text = time.ToString();
}
}
{
if ( ! Page.IsPostBack)
{
// 显示用户配置信息
DisplayProfileInfo();
}
}
protected void btnSubmit_Click( object sender, EventArgs e)
{
// 保存用户配置信息到Profile属性中
Profile.Name = txtName.Text;
Profile.Address.City = txtCity.Text;
Profile.Address.PostalCode = txtPostalCode.Text;
Profile.LastSubmit = DateTime.Now;
// 显示用户配置信息
DisplayProfileInfo();
}
private void DisplayProfileInfo()
{
// 从Profile属性中获取数据并赋值给服务器控件
txtName.Text = Profile.Name;
txtCity.Text = Profile.Address.City;
txtPostalCode.Text = Profile.Address.PostalCode;
DateTime time = Profile.LastSubmit;
// 如果未获取值则显示空,否则显示获取的值
if (time.Year == 1 )
{
labLastSubmit.Text = " 空 " ;
}
else
{
labLastSubmit.Text = time.ToString();
}
}
2、为注册用户实现个性化用户配置
web.config配置
<
connectionStrings
>
< add name ="NorthwindConnectionString" connectionString ="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
providerName ="System.Data.SqlClient" />
</ connectionStrings >
< system.web >
< profile >
< properties >
< add name ="ShoppingCart" type ="ShoppingCart" serializeAs ="Binary" />
</ properties >
</ profile >
< authorization >
< deny users ="?" />
</ authorization >
< authentication mode ="Forms" >
< forms loginUrl ="Login.aspx" ></ forms >
</ authentication >
< add name ="NorthwindConnectionString" connectionString ="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True"
providerName ="System.Data.SqlClient" />
</ connectionStrings >
< system.web >
< profile >
< properties >
< add name ="ShoppingCart" type ="ShoppingCart" serializeAs ="Binary" />
</ properties >
</ profile >
< authorization >
< deny users ="?" />
</ authorization >
< authentication mode ="Forms" >
< forms loginUrl ="Login.aspx" ></ forms >
</ authentication >
代码示例:code 13-2
3、匿名用户转化为注册用户的处理
Global.asax中的设置
void
Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
{
//获取匿名用户的Profile对象
ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);
//如果总价为不为0(说明匿名用户进行了选择),则将匿名用户的Profile存储起来
if (anonProfile.ShoppingCart.Total != 0)
{
Profile.ShoppingCart = anonProfile.ShoppingCart;
}
//删除匿名用户的用户数据(从aspnet_Users表)
Membership.DeleteUser(pe.AnonymousID);
//删除匿名用户的Profle数据(从aspnet_Profile表)
ProfileManager.DeleteProfile(pe.AnonymousID);
//删除匿名用户标识
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}
{
//获取匿名用户的Profile对象
ProfileCommon anonProfile = Profile.GetProfile(pe.AnonymousID);
//如果总价为不为0(说明匿名用户进行了选择),则将匿名用户的Profile存储起来
if (anonProfile.ShoppingCart.Total != 0)
{
Profile.ShoppingCart = anonProfile.ShoppingCart;
}
//删除匿名用户的用户数据(从aspnet_Users表)
Membership.DeleteUser(pe.AnonymousID);
//删除匿名用户的Profle数据(从aspnet_Profile表)
ProfileManager.DeleteProfile(pe.AnonymousID);
//删除匿名用户标识
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}
示例代码:code 13-3
4、删除个性化信息
删除匿名用户的个性化信息
ProfileManager.DeleteProfile(Context.Request.AnonymousID)
删除注册用户的个性化信息
ProfileManager.DeleteProfile(User.Identity.Name)