Implementing User Profiles in ASP.NET – A Beginner’s Guide

This article discusses the basic of User Profiles. Why do we need user profiles and how to implement user profiles in ASP.NET.


Websites that provide users a possibility to customize the look and feel of a website are often liked by the user. It gives a little personal touch to the web site from a users’ perspective.

ASP.NET themes is one way to provide users an option to change the appearance of the website. The other important aspect of it is remembering a returning user and show him a customized layout based on his preferences.

User profiles are used to achieve the same. Using profiles we can remember the user between visits and display the appropriate layout/information to him.

Using the code

Understanding the Basics

To implement user profiles we need to specify a some things. Typical process of implementing User profiles will follow the below mentioned steps

  1. First we need to specify where the user profile will be saved. this can be done by specifying the Profile Provider.
  2. Once we have the Profile provider configured, we need to decide what information we need to save for the user i.e. we need to define the user profile.
  3. After this we need a mechanism to identify the user uniquely. This is easy if the user is a member of our site but it gets tricky when we want to track all the anonymous users too.
  4. Once we have identified the users uniquely we need to take his preferences and store them in our profile provider.
  5. Last but not the lease we need a way to identify the returning users and change the site’s layout as per his saved preferences.

Let us now develop a small application that will remember the user’s name and the time of his last visit.

The default settings of profile is such that it will work only for authenticated users i.e. only for the members of the site. we will see how to do this for the anonymous users. then we will see how we can change this to use the authenticated users.

Note: This application will contain a single page website to demonstrate the User profiles. Typical real world usage of user profile will be on a larger scale and perhaps it will be used in conjunction with ASP.NET themes and/or web parts.

Configuring the Profile Provider

The profile provider specifies the store where the user preferences will be stored. Typically this is SqlServerdatabase. This needs to be done in the web.config file.

The settings above shows the default profile provider which is SqlServer database placed in the App_Data directory of the website. If we need to specify the custom profile provider then we need to specify the connectionStringintead of connectionStringNameto use the appropriate database.

Also, we need to set the database to accept and save the profile related data. this can be done by running the aspnet_regsql command against the database that needs to be used to save the profiles’ data.

Specifying the User Profile Properties

Now we have the provider configured, we need to specify the data that we need to save for the users. We can do this by having the properties in the provider element of web.config. So let us create the entries in web.config to track the required information that we discussed above.

The allowAnonymous="true" specifies that the anonymous users should also be able to save their profile information. For this to work properly we need to specify <anonymousIdentification enabled="true"/> in the web.config file.

Once we are done with this we have successfully defined what all properties we need to track as the user profile information and it will work for authenticated as well as anonymous users.

Accessing and Saving the Profile information

Once we have done this the ASP.NET framework will create strongly type properties for each of our profile properties. If we need to access these profile properties we can use the Profile.<PropertyName> syntax to do so. If we need to access the properties we defined above all we need to do

The other part of it is saving the Profile information. To do that we can simply assign the new values to these properties and make our changes permanent by calling Profile.Save(). Lets see how this can be done with our properties.

The important thing to note while accessing the profiles is that if user has not saved his values yet then they will result the default values of their respective types.

Get our sample Application to work

In the application that we are writing we will show the username and the time of his last visit. For the first visit both of them will be default values. From the next time onwards, if the user has saved his name then it will show the name otherwise it will simply show the time of last visit.

Let us see the design view of our page:


Now let us put all the above seen code to achieve the desired functionality, this is how our code behind will look like:

Let us now run the page for the first time:


Lets now save our preferences and run it again:


So this time we can see that it remembered the name I saved and also shows me the last access time. The User profiles have been configured and working fine now.

A note on Cookies

All the user profiles functionality that we have seen can also be done using cookies. Doing that will give complete control in developers hands. in fact ASP.NET actually uses cookiesto track the visits of anonymous user. SO if we are saving the profiles for anonymous user and user decide to delete his cookiesthen his preferences will not be accessible to him i.e. he will not be identified as a returning user.

Points of Interest

User profiles provide a very good way of identifying the returning users. Cookiescan also be used for the same thing. But using Profiles we don’t have to write that much code that we would have to write if we use cookies. Also This approach is less error prone and using cookies. But I still think that knowing all about cookies in details is a very must thing for a web developer. Perhaps I will talk about cookiesin a separate article.

This article is written from the perspective of absolute beginners. I hope this has been informative.

Download sample code for this article: userProfilesDemo