Welcome to TheCredence.com - You may like to subscribe to our RSS feed to stay updated.
As promised I am going to cover some semi-advanced articles on ASP. I have already wrote one article on Authentication and security in ASP last week. Here is one more on Built In Personalization Techniques in ASP.Net 2.0
One of the most important feature that professional sites need to have implemented is personalization. If you look at a site you have already visited you will surprise how many things it knows about you. For example your name, address, visits count, ip address, your favourite theme. In order to be implemented personalization, another technology must be provided. That technology is called profiling. This is a way site to collect as many information about particular user as needed, just to provide comfortable environment. Some years ago coders had to write so much code to implement such technique. First they had to create database, then database fill, extract and UI programming to match the user preference.
Now ASP.NET 2.0 provides built-in features to implement personalization with just a little need of code.
How Personalization works in ASP
User profiling build starts with defining the profile properties in the Web.config file. It looks like this:
<profile automaticSaveEnabled="true"> <properties> <add name="Visits" type="System.Int32"/> <add name="Name" type="System.String"/> <add name="Theme" type="System.String"> <add name="email" type="System.String"> </properties> </profile>
In other words this is the profile schema.
Above properties can be access in the code through the object Profile of the class ProfileCommon. So if we need to do something like showing the user name when open a page will look like:
protected void Page_Load(object sender, EventArgs e) { if (Profile.Name != null) { lblUserName.Text = Profile.Name; } }
The same is if we want to apply the user favourite theme to the site. (It must be done in Preinit event handler.) Transfer data from environment to profile is valid too.
Profile.email = txtMail.Text;
AutomaticSaveEnabled property should be set to true and the ASP.NET concers for the saving of the profile properies.
By default personalization is valid only for authenticated users, but guest can be profiled too. Tracking the anonimous personalization is implemented by cookie.
Build in Profile provider described above is very good because no code is needed to implement it, Just work with it. But imagine you need the stored data to be available to another application. It need very complicated code written to read the data. Instead you may use another data provider or build a custom one.
Using SqlProfileProvider for Personalization
This provider serves the ability coder to store profile data in a SQL 7.0 or later database. Thus the data can be easily reused by other applications. Using the SqlProfileProvider needs following steps completed:
1. Crate profile tables.
To create profile tables a tool named aspnet_regsql.exe is used. Created tables can be used by more than one application. The tool also create the necessary stored procedures to work with the profiles.
2. Configure the provider.
The configuration should look like this:
<connectionStrings> <add name="SqlServices" connectionString= "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" /> </connectionStrings> <system.web> <profile defaultProvider="SqlProvider"> <providers> <clear /> <add name="SqlProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="SqlServices" applicationName="MyApplication" /> </providers> </profile> ... </system.web>
3. Define the profile properties. It is the same as the basic provider but this should be mentioned.
<profile defaultProvider="SqlProvider">
4. Using properties.
Before using the properties, coder should provide authorization of users. Any kind of authentication system is valid, simply an authorization rule must be provided. For example:
<system.web> <authentication mode="Windows"/> <authorization> <deny users="?"/> </authorization> ... </system.web>
Working with Profile object in code is the same as described earlier in the article. I hope this article will give you basic knowledge of Personalization aspects in ASP.Net 2.0. Wait for few more to come..
Links you may find interesting -
