Overview of Personalized User Configuration
1. <profile> configuration section
When setting the <profile> configuration section, three parts are often configured:
1. <profile> own attribute settings
2. <profile> configuration section byte <properties> property settings
3. <profile> configuration section sub-node <providers> attribute settings
2. <profile> configuration section
add in 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>
3. Personalized user configuration API
Create a Login.aspx page as the user login page
Write the code in Default.aspx as follows:
Response.Write("昵称:" + Profile.username + "<br/>密码:" + Profile.userpwd + );
Add the AddProfile.aspx page to handle user personalization settings. The following is a code example:
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"); }
Create Welcome.aspx as follows:
Fourth, the realization of anonymous personalization
Modify the code in web.config as follows (purple is the code to pay attention to):
<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>-->
Modify the code of AddProfile.aspx as follows:
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"); }
Modify the code of login.aspx as follows:
protected void Page_Load(object sender, EventArgs e) { Response.Write("昵称:" + Profile.username + "<br/>密码:" + Profile.userpwd + "<br/>生日:"+Profile.bridaty.ToLongDateString()); }