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: 1144
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:
- Membership tables installed? Check.
- Profile enabled in web.config? Check.
- SqlProfileProvider connection string set? Check.
- Profile properties defined in said web.config file? Check.
- 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:
- using System.Web.Profile;
- using System.Web.Security;
- using Project.Core;
- namespace Project.Web.Context
- {
- public class MemberPreferencesProfile : ProfileBase
- {
- static public MemberPreferencesProfile CurrentUser
- {
- get
- {
- return (MemberPreferencesProfile)
- Create(Membership.GetUser().UserName);
- }
- }
- public Enums.PresenceViewModes? ViewMode
- {
- get { return ((Enums.PresenceViewModes)
- ( base["ViewMode"] ?? Enums.PresenceViewModes.Category)); }
- set { base["ViewMode"] = value; Save(); }
- }
- }
- }
And then I replaced the existing profile configuration web.config with the following:
- <profile enabled="true" defaultProvider="MvcSqlProfileProvider"
- inherits="Project.Web.Context.MemberPreferencesProfile">
- <providers>
- <clear/>
- <add name="MvcSqlProfileProvider"
- type="System.Web.Profile.SqlProfileProvider, System.Web,
- Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
- connectionStringName="ApplicationServices" applicationName="/"/>
- </providers>
- </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:
- viewMode = MemberPreferencesProfile.CurrentUser.ViewMode;
- MemberPreferencesProfile.CurrentUser.ViewMode = viewMode;
© Johnny Coder or respective owner