ASP.NET MVC Custom Profile Provider

Posted by Ben Griswold on Johnny Coder See other posts from Johnny Coder or by Ben Griswold
Published on Fri, 02 Apr 2010 00:36:53 +0000 Indexed on 2010/04/02 0:44 UTC
Read the original article Hit count: 1104

Filed under:
|
|

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;
  4.  
  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.         }
  17.  
  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=2.0.0.0, 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;

© Johnny Coder or respective owner

Related posts about ASP.NET

Related posts about ASP.NET MVC