个性化用户配置概述
一、<profile>配置节
设置<profile>配置节时,经常对其中的三部分进行配置:
1.<profile>自身属性设置
2.<profile>配置节的字节<properties>属性设置
3.<profile>配置节的子节点<providers>属性设置
二、<profile>配置节
在web.config中添加<profile enabled="true" defaultProvider="AspProfileProvider"> <providers> <clear/> <add name="AspProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString"/> </providers> <properties> <add name="username" allowAnonymous="true"/> <add name="userpwd" allowAnonymous="true"/> <add name="bridaty" type="System.DateTime" allowAnonymous="true"/> </properties> </profile> <authentication mode="Forms"> <forms loginUrl="Login.aspx" defaultUrl="Welcome.aspx"> </forms> </authentication> <authorization> <deny users="?"/> </authorization>
三、个性化用户配置API
创建一个Login.aspx页面,作为用户登录页面
在Default.aspx中写代码如下:
Response.Write("昵称:" + Profile.username + "<br/>密码:" + Profile.userpwd + );
添加AddProfile.aspx页面,处理用户个性化设置,以下为代码实例:
protected void Button1_Click(object sender, EventArgs e) { if (!Profile.IsAnonymous) { Profile.username = TextBox1.Text; Profile.userpwd = TextBox2.Text; Profile.bridaty = Calendar1.SelectedDate; Response.Redirect("Default.aspx"); }
创建Welcome.aspx如下:
四、匿名个性化的实现
修改web.config中的代码如下(紫色为须注意的代码):
<anonymousIdentification enabled="true"/> <profile enabled="true" defaultProvider="AspProfileProvider"> <providers> <clear/> <add name="AspProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="aspnetdbConnectionString"/> </providers> <properties> <add name="username" allowAnonymous="true"/> <add name="userpwd" allowAnonymous="true"/> <add name="bridaty" type="System.DateTime" allowAnonymous="true"/> </properties> </profile> <authentication mode="Forms"> <forms loginUrl="Login.aspx" defaultUrl="Welcome.aspx"> </forms> </authentication> <!--<authorization> <deny users="?"/> </authorization>-->
修改AddProfile.aspx的代码如下:
protected void Button1_Click(object sender, EventArgs e) { if (!Profile.IsAnonymous) { Profile.username = TextBox1.Text; Profile.userpwd = TextBox2.Text; Profile.bridaty = Calendar1.SelectedDate; Response.Redirect("Default.aspx"); } else { Profile.username = TextBox1.Text; Profile.userpwd = TextBox2.Text; Response.Redirect("Default.aspx"); }
修改login.aspx的代码如下:
protected void Page_Load(object sender, EventArgs e) { Response.Write("昵称:" + Profile.username + "<br/>密码:" + Profile.userpwd + "<br/>生日:"+Profile.bridaty.ToLongDateString()); }