Vous êtes sur la page 1sur 6

Implementing User Profiles in ASP.

NET - A
Beginner's Guide
Rahul Rajat Singh, 4 Sep 2012

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

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

Background
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.configfile.

<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer"

applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web,


Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>

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

connectionStringName

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.
<profile>
<properties>
<add name="Name" allowAnonymous="true"/>
<add name="VisitedOn" type="System.DateTime" allowAnonymous="true"/>
</properties>
</profile>

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;
Profile.Save();

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";
}
else
{
//returning user, show his name
Label1.Text = name;
}

if (lastVisited.ToString() == "1/1/0001 12:00:00 AM")


{
Label2.Text = "Never";
}
else
{
Label2.Text = lastVisited.ToString();
}
}
}

protected void Page_UnLoad(object sender, EventArgs e)


{
Profile.VisitedOn = DateTime.Now;
Profile.Save();
}
protected void Button1_Click(object sender, EventArgs e)
{
Profile.Name = TextBox1.Text;
Profile.Save();
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.

Cookies

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.

Vous aimerez peut-être aussi