Click here to Skip to main content
13,557,707 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


39 bookmarked
Posted 11 Jul 2012
Licenced CPOL

Implementing User Profiles in ASP.NET - A Beginner's Guide

, 4 Sep 2012
Rate this:
Please Sign up or sign in to vote.
This article discusses the basic of User Profiles. Why do we need user profiles and how to implement user profiles in ASP.NET.


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 SqlServer database. This needs to be done in the web.config file. 

		<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" 

                applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

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 connectionString intead of

to 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. 

		<add name="Name" allowAnonymous="true"/>		
		<add name="VisitedOn" type="System.DateTime" allowAnonymous="true"/>			

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 

string name = Profile.Name;            
DateTime lastVisited = Profile.VisitedOn;

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.

Profile.Name = TextBox1.Text;
Profile.VisitedOn = DateTime.Now;

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:

protected void Page_Load(object sender, EventArgs e)
    if (IsPostBack == false)
        string name = Profile.Name;            
        DateTime lastVisited = Profile.VisitedOn;

        if (name == string.Empty)
            //User has not specified his name
            Label1.Text = "Guest";
            //returning user, show his name
            Label1.Text = name;

        if (lastVisited.ToString() == "1/1/0001 12:00:00 AM")
            Label2.Text = "Never";
            Label2.Text = lastVisited.ToString();

protected void Page_UnLoad(object sender, EventArgs e)
    Profile.VisitedOn = DateTime.Now;
protected void Button1_Click(object sender, EventArgs e)
    Profile.Name = TextBox1.Text;
    Label1.Text = TextBox1.Text;

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 cookies to track the visits of anonymous user. SO if we are saving the profiles for anonymous user and user decide to delete his cookies then 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.

can 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 cookies in a separate article. 

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


  • 12 July 2012: First Version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahul Rajat Singh
Software Developer (Senior)
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more:[^]

  • Microsoft MVP 2015

You may also be interested in...


Comments and Discussions

AnswerArticle of the Day on Microsoft's site Pin
Rahul Rajat Singh22-Dec-12 18:19
memberRahul Rajat Singh22-Dec-12 18:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 4 Sep 2012
Article Copyright 2012 by Rahul Rajat Singh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid