How Do I Access the Profile of a Different User Directly in C#?

You may be aware of the new ASP.NET 2.0 Profile feature that allows you to store and retrieve information for the currently logged on user. This Profile feature makes it extremely simple to store user specific information, like a user's address, phone number, or site preferences. (Note: if you're not familiar with ASP.NET 2.0 Profiles check out the section Storing User Profiles in the ASP.NET Quick starts).

But what if you want to access the Profiles data for another user? For example, what if you want to allow a site administrator to change the personal data of all users in your site? The trick to make this possible is to use the ProfileCommon class, which inherits from ProfileBase. This article shows you how you can use this class to access the Profile data for arbitrary users.

Accessing the Current's User's Profile Data

(Note that you can also access a Visual Basic .NET version of this article).

Normally, you can access the Profile data from the Profile class that .NET compiles for you on the fly based on the information stored in the <properties> node under <profile> in the Web.config file. Imagine that there is a key called Street of type String in the Web.config (the sample application that you can download at the end of this article has a Web.config file with the property called Street already set up). You'd access the property like this:

  // Access the property  
  Profile.Country = "SomeStreet";

  // Change its value
  myStreet = Profile.Country;
This gives you strongly typed access to the Profile's properties that are automatically retrieved from and saved to the backing data store, like a SQL Server database.

Accessing a Different User's Profile Data

If you have, for example, a page in the Management section of your site that you use to update the details for one or more users in your database, this code won't work. Every time this code runs, it will only update the current user's Profile; that is, it will only update the Administrator's Profile. To get access to a different Profile for an arbitrary user, you need to create one yourself, and then access its properties. Notice that the term create is a bit of a misnomer. You create an instance of the ProfileBase class, but you don't necessarily create the Profile itself. Only the very first time, when the Profile for the requested user does not exist yet, is the Profile created. In other cases, the existing Profile is retrieved from the database and used. Take a look at the following code to see how this works. First you see the ASPX portion of the code, then you'll see the C# code in the Code Behind file:

<h3>Create New Profile</h3>
  User Name 
  <asp:TextBox ID="txtUserName1" runat="server"></asp:TextBox><br />
  <asp:TextBox ID="txtStreet" runat="server"></asp:TextBox><br />
  <asp:Button ID="btnSave" runat="server" Text="Create Profile" 
        OnClick="btnSave_Click" /><br />
  <br />
  <br />
<h3>Retrieve Existing Profile</h3>
  User Name
  <asp:TextBox ID="txtUserName2" runat="server"></asp:TextBox><br />
  <asp:Button ID="btnRetrieve" runat="server" Text="Retrieve Profile" 
        OnClick="btnRetrieve_Click" /><br />
  <br />
  <asp:Label ID="lblStreet" runat="server"></asp:Label>

This page contains two areas: one that allows you to create a new Profile by entering a user name and the name of the street. Once you click the Create Profile button, either a new Profile is created or an existing Profile is retrieved from the database for the user name specified in the TextBox txtUserName1. The Profile is then updated with the new information from the txtStreet TextBox.

Once you click the Retrieve Profile button in the second area of the page, the existing Profile is retrieved from the data store, and the Label called lblStreet is updated with the user's data.

To see how this works, take a look at the Code Behind for the page. You'll see two event handlers that deal with the Click events for the two buttons. First, take a look at the code for the Create Profile button:

protected void btnSave_Click(object sender, EventArgs e)
  ProfileCommon userProfile = (ProfileCommon) 
          ProfileCommon.Create(txtUserName1.Text, true);
  userProfile.Street = txtStreet.Text;

The first line in this method uses the Create method defined in the ProfileBase class to create a new instance of the ProfileBase class. You'll then need to cast it to a ProfileCommon to access the custom properties defined in the Web.config file. As the first argument, the name of the Profile to create is passed. The second argument determines whether the user is treated as authenticated or not. When you pass false instead, the user will be treated as not authenticated, and you won't be able to access properties that are only accessible by authenticated users (that is, properties that have allowAnonymous="false" in the Web.config).

Once you have a reference to the user's Profile, setting properties is as simple as is normally the case. The only caveat is that you must call Save() on the Profile or otherwise the changes are not persisted. Normally, the ASP.NET 2.0 runtime does this for you automatically in the EndRequest event at the end of the execution cycle of an ASP.NET page, provided that the AutomaticSaveEnabled property is set to True , but with this code, you need to call it yourself.

The final step is to retrieve the data again. Not surprisingly, this code is almost identical:

protected void btnRetrieve_Click(object sender, EventArgs e)
  ProfileCommon userProfile = (ProfileCommon) 
            ProfileCommon.Create(txtUserName2.Text, true);
  lblStreet.Text = userProfile.Street;

This code creates a new ProfileCommon instance again and then accesses its Street property to update the label.

Accessing the current's user's Profile was already extremely simple, but with the bit of code from this article, accessing arbitrary Profiles is now just has easy. Although this short article demonstrated a quite simple example, it is easy to apply the same principles to everything you know about Profiles, including grouped properties.

Download Files

The download for this article comes as an ASP.NET 2.0 web site with a simple page that demonstrates the code you just saw, and a Web.config file that defines a simple Street property. You'll notice that the download doesn't contain the database; when you run this page, the .NET Framework will automatically create and attach a new database for you that is configured to support the Profiles feature.

Where to Next?

Wonder where to go next? You can post a comment on this article.

Doc ID 376
Full URL
Short cut
Written by Imar Spaanjaars
Date Posted 01/03/2006 23:06
Date Last Updated 05/21/2006 20:07
Date Last Reviewed 05/21/2006 20:07
Listened to when writing Mein Herz Brennt by Rammstein (Track 1 from the album: Mutter)


Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.