ASP.NET: Accessing other User's Profiles

by Brad 4. February 2008 15:56

One of the many improvements in ASP.NET 2.0 was the Provider based architecture, and the most under used (certainly by me) is the User Profile Provider (click here for more info). You simply define the properties you want your users to have in the web.config (see below)

<profile enabled="true" defaultProvider="MyProvider">
     <providers>
       <add name="MyProvider" connectionStringName="MyConnection"
            applicationName="/" 
            type="System.Web.Profile.SqlProfileProvider" />
     </providers>
     <properties>
       <add name="FirstName" type="string" />
       <add name="LastName" type="string" />
     </properties>
   </profile>

And .NET takes care of the rest... you can then access the profile programmatically as follows:

string firstname = Profile.FirstName;
string lastname = Profile.LastName;

Simple!

There are a couple of problems using the SqlProfileProvider, firstly the data is serialised and stored in one field in the database - making it trickier to run SQL queries on it... and the second (or so I thought until recently) is accessing other user's profile data - for example an administrator should be able to see the other user's FirstName and LastName values.

However the second issue isn't a problem at all, it is in fact possible to view/edit another user's profile with the following code:

ProfileCommon userProfile = (ProfileCommon)ProfileCommon.Create("Brad", true);
userProfile.FirstName = "Paul";
userProfile.LastName = "Bradley";
userProfile.Save();

Tags:

C#

Comments

2/7/2008 11:00:49 AM #

Hello! Me Again

When doing the MCTS Web Exam, I learned about profiles, and did you know that you can add anonymous profiles items as well, so that even if users do not log into your site, you can still personalize the site for them!

It remembers their info via cookies and using the in-built Personalization providers, but is very handy if you want to remember themes or last visit dates.

Dom

Dominic |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar