ASP.NET MVC Custom Profile Provider

It’s been a long while since I last used the ASP.NET Profile provider. It’s a shame, too, because it just works with very little development effort:

    1. Membership tables installed? Check.
    2. Profile enabled in web.config? Check.
    3. SqlProfileProvider connection string set? Check. 
    4. Profile properties defined in said web.config file? Check.
    5. Write code to set value, read value, build and test. Check. Check. Check. 

Yep, I thought the built-in Profile stuff was pure gold until I noticed how the user-based information is persisted to the database. It’s stored as xml and, well, that was going to be trouble if I ever wanted to query the profile data.  So, I have avoided the super-easy-to-use ASP.NET Profile provider ever since, until this week, when I decided I could use it to store user-specific properties which I am 99% positive I’ll never need to query against ever. 

I opened up my ASP.NET MVC application, completed steps 1-4 (above) in about 3 minutes, started writing my profile get/set code and that’s where the plan broke down.  Oh yeah. That’s right.  Visual Studio auto-generates a strongly-type Profile reference for web site projects but not for ASP.NET MVC or Web Applications.  Bummer. So, I went through the steps of getting a customer profile provider working in my ASP.NET MVC application:

First, I defined a CurrentUser routine and my profile properties in a custom Profile class like so:

  1. using System.Web.Profile;
  2. using System.Web.Security;
  3. using Project.Core;
  5. namespace Project.Web.Context
  6. {
  7.     public class MemberPreferencesProfile : ProfileBase
  8.     {
  9.         static public MemberPreferencesProfile CurrentUser
  10.         {
  11.             get
  12.             {
  13.                 return (MemberPreferencesProfile)
  14.                     Create(Membership.GetUser().UserName);
  15.             }
  16.         }
  18.         public Enums.PresenceViewModes? ViewMode
  19.         {
  20.             get { return ((Enums.PresenceViewModes)
  21.                     ( base["ViewMode"] ?? Enums.PresenceViewModes.Category)); }
  22.             set { base["ViewMode"] = value; Save(); }
  23.         }
  24.     }
  25. }

And then I replaced the existing profile configuration web.config with the following:

  1. <profile enabled="true" defaultProvider="MvcSqlProfileProvider"
  2.          inherits="Project.Web.Context.MemberPreferencesProfile">     
  3.   <providers>
  4.     <clear/>
  5.     <add name="MvcSqlProfileProvider"
  6.          type="System.Web.Profile.SqlProfileProvider, System.Web,
  7.          Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  8.          connectionStringName="ApplicationServices" applicationName="/"/>
  9.   </providers>
  10. </profile>

Notice that profile is enabled, I’ve defined the defaultProvider and profile is now inheriting from my custom MemberPreferencesProfile class. 

Finally, I am now able to set and get profile property values nearly the same way as I did with website projects:

  1. viewMode = MemberPreferencesProfile.CurrentUser.ViewMode;
  2. MemberPreferencesProfile.CurrentUser.ViewMode = viewMode;


  1. Hi !

    i want to ask you about technical things in The code: MemberPreferencesProfile>

    What do you mean by “Project” is it the name of the hole project ?
    if yes, why i couldn’t find “MyProjcet.Core” ? Personally i’ve never seen “.Core”. What is it ?

    Finally, [… Enums.Presenc….] is underlined, it says : “a Using directive is missing”! Could any one, Please, answer to these Questions, i really need this code working , to accomplish an urgent task.

    Thanks !


  2. “Project” is the project-specific, root namespace I am using in this example so it will not exist in your solution. The same for the Enums reference. That’s specific to my solution as well. In order to get this code running in your solution, follow the steps I have outlined. You should remove the “ViewMode” property, the using Project.Core statement and you should rename the “Project” namespace to the root namespace of your project. I hope that makes sense. Best of luck.

  3. Hello,
    In your opinion is it better to go through the extra steps and use membership profile mechanism to add profile info to users or just use entity frameworks code first method in MVC3.