默认情况,ASP.NET2.0没有开启匿名用户个性化设置,通过Web.config设置,可以实现对匿名用户的支持。
Web.config设置
<?
xml version="1.0"
?>
< configuration xmlns ="http://schemas.microsoft.com/.NetConfiguration/v2.0" >
< appSettings />
< connectionStrings />
< system .web >
< 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 ="ZipCode" allowAnonymous ="true" />
</ group >
</ properties >
</ profile >
< compilation debug ="true" />
< authentication mode ="Windows" />
</ system.web >
</ configuration >
< configuration xmlns ="http://schemas.microsoft.com/.NetConfiguration/v2.0" >
< appSettings />
< connectionStrings />
< system .web >
< 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 ="ZipCode" allowAnonymous ="true" />
</ group >
</ properties >
</ profile >
< compilation debug ="true" />
< authentication mode ="Windows" />
</ system.web >
</ configuration >
anonymousIdentification:针对应用程序授权配置匿名标识。这是在需要授权时,对没有经过身份验证的实体进行标识所必需的。
allowAnonymous必须设置为"True"。
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
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.ZipCode = txtPostalCode.Text;
Profile.LastSubmit = DateTime.Now;
//显示用户配置信息
DisplayProfileInfo();
}
private void DisplayProfileInfo()
{
//从Profile属性中获取数据并赋值给服务器控件
txtName.Text = Profile.Name;
txtCity.Text = Profile.Address.City;
txtPostalCode.Text = Profile.Address.ZipCode;
DateTime time = Profile.LastSubmit;
//如果未获取值则显示空,否则显示获取的值
if (time.Year == 1)
{
labLastSubmit.Text = "空";
}
else
{
labLastSubmit.Text = time.ToString();
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
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.ZipCode = txtPostalCode.Text;
Profile.LastSubmit = DateTime.Now;
//显示用户配置信息
DisplayProfileInfo();
}
private void DisplayProfileInfo()
{
//从Profile属性中获取数据并赋值给服务器控件
txtName.Text = Profile.Name;
txtCity.Text = Profile.Address.City;
txtPostalCode.Text = Profile.Address.ZipCode;
DateTime time = Profile.LastSubmit;
//如果未获取值则显示空,否则显示获取的值
if (time.Year == 1)
{
labLastSubmit.Text = "空";
}
else
{
labLastSubmit.Text = time.ToString();
}
}
}
以上代码出于《asp.net2.0开发指南》。