Websites are designed for repeat visits from users. Personalization allows a website to remember user identification and other information details, and it provides a personal context for each user.
ASP.NET provides services for personalizing a website to suit the tastes and preferences of specific customers.
ASP.NET personalization services are based on the user's profile. The user profile defines the information that the website requires from the user. For example, name, age, address, date of birth and mobile phone number.
This information is defined in the application's web.config file and the ASP.NET runtime reads and uses it. This work is done by the personalization provider.
The user profiles contained in the user data are stored in the default database created by ASP.NET. You can create your own database to store signature files. Profile data definitions are stored in the configuration file web.config.
Let us create a sample website where we want our application to remember user details like name, address, date of birth, etc. Add profile details using the element in the web.config file.
<configuration><system.web><profile> <properties> <add name="Name" type ="String"/> <add name="Birthday" type ="System.DateTime"/> <group name="Address "> <add name="Street"/> <add name="City"/> <add name="State"/> <add name="Zipcode"/> </group> </properties></profile></system.web></configuration>
When a profile is defined in the web.config file, the profile is available through the Profile attribute found in the current HttpContext and available through the page.
Add a text box to capture user input defined in the feature file, and a button to submit the data:
Update Page_load to display feature file information:
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 (!this.IsPostBack) { ProfileCommon pc=this.Profile.GetProfile(Profile.UserName); if (pc != null) { this.txtname.Text = pc.Name; this.txtaddr.Text = pc.Address.Street ; this.txtcity.Text = pc.Address.City; this.txtstate.Text = pc.Address.State; this.txtzip.Text = pc.Address.Zipcode; this.Calendar1.SelectedDate = pc.Birthday; } } }}
Write the following handle for the submit button and store the user data in the feature file:
protected void btnsubmit_Click(object sender, EventArgs e){ ProfileCommon pc=this.Profile.GetProfile(Profile.UserName); if (pc != null) { pc.Name = this.txtname.Text; pc.Address.Street = this .txtaddr.Text; pc.Address.City = this.txtcity.Text; pc.Address.State = this.txtstate.Text; pc.Address.Zipcode = this.txtzip.Text; pc.Birthday = this.Calendar1.SelectedDate; pc.Save(); }}
When the page is executed for the first time, the user needs to enter information. However, the next time the user's details will be loaded automatically.
In addition to the name and type attributes we have already used, elements have other attributes. The following table shows some of these properties:
property | describe |
---|---|
name | The name of the property. |
type | The type defaults to string but it allows any complete class name as the data type. |
serializeAS | The format to use when serializing this value. |
readOnly | Read-only signature file values cannot be changed. This property defaults to false. |
defaultValue | A default value that is used if the signature file does not exist or has no information. |
allowAnonymous | A Boolean value indicating whether this attribute can be used with anonymous files. |
Provider | The profile provider that should be used to manage this property. |
Anonymous personalization allows users to personalize a website before identifying themselves. For example, Amazon.com allows users to add items to their shopping cart before logging in. To enable this feature, the web.config file can be configured as follows:
<anonymousIdentification enabled ="true" cookieName=".ASPXANONYMOUSUSER" cookieTimeout="120000" cookiePath="/" cookieRequiresSSL="false" cookieSlidingExpiration="true" cookieprotection="Encryption" coolieless="UseDeviceProfile"/>