ContentProfileProvider

Dec 19, 2008 at 4:33 PM
Hi

Does anyone have a sample using ContentProfileProvider in a addon?

Dec 19, 2008 at 5:20 PM
I've recently used it when implementing an AuthorProfile functionality for "BlogEngine.Net on top of N2" excercise, you can find links to sources and project in the Issue Tracker. Not a simple stuff, sorry.
Dec 19, 2008 at 6:51 PM

Thanks for your answer esteewhy

I think it is to big for be to look at. I trying to make the addon in VB.
What I want is to extend users with more info like Firstname and lastname. I need find a way to doit either via profiles or extend the user class.

Dec 19, 2008 at 7:43 PM
Edited Dec 19, 2008 at 7:46 PM
Okay, i see, so here's how to start tackling this:

Firstly, it's definetly worth considering a standard ASP.Net profile functionality instead of extending N2's internal user class, because of the flexibility the former gives you.

There are two ways of using profile properties. The one i've utilised in my sample looked like this:

1) Configure a profile provider and define properties inside your web.config file:

...
<profile defaultProvider="MyProvider">
<providers>
<clear/>
<add name="MyProvider"
type="N2.Security.ContentProfileProvider, N2.Security"/>
</providers>
<properties>
<add name="FirstName"/>
<add name="LastName" type="System.String"/>
...
2) Access properties via generic profile class as if it was a collection of properties (i'll "speak" c#3, but hopefully you'll graps it):
..
using System.Web.Profile;
..
//get a profile for a given user
//note, it doesn't "create" it really, just retrieve one..
var _userName = "user";
var _profile = ProfileBase.Create(_userName);
..
//read properties
var _firstName = (string)_profile["FirstName"];
var _lastName = (string)_profile["LastName"];
..
//another time you might like to write them:
_profile["FirstName"] = "Clueless";
_profile["LastName"] = "Morgan";
_profile.Save();
..
That's it. Just watch out to define every property in the web.config, otherwise you're risking to get a run-time exception on trying to access it.
Another, a lot better approach is to ..

1) .. design your own profile base class:

public class MyProfile: BaseProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
2) Configure the provider in web.config, leaving properties aside:
...
<profile defaultProvider="MyProvider" inherits="MyProfile">
...
3) Now accessing properties is getting more pleasant:
...
var _profile = ProfileBase.Create(_userName) as MyProfile;
...
var _firstName = _profile.FirstName;
...
Dec 19, 2008 at 7:58 PM

Thanks a lot, that helped me to understand it.

I'll go for the "create my own" profile".
In 1) your are making "Inherits BaseProfile". What is baseprofile? Is that a part of N2?

/Michael

Dec 19, 2008 at 9:45 PM
What is baseprofile? Is that a part of N2?
No no no! BaseProfile lives in System.Web.Profile since .Net 2.0. That's an essense of all the hype with ASP.Net profiles -- you're absolutely independent of N2 and can switch to any other profile provider by just setting ... defaultProvider="MyOtherProvider" ... in the web.config.

If you want more info, you can further look at \WINDOWS\Microsoft.Net\Framework\v2*\config\machine.config -- that's where default profile provider is defined.

Another part of the story is that if your solution is limited to a single stand-alone "web site"/"web project" (that is, the code that uses profiles is in the same "project" as your .aspx/.ascx stuff) then you can avoid creating your custom profile class and use one that is created automatically by ASP.Net: this class is named "ProfileCommon" and contains all the properties you put into wab.config. Also, it's instance for a current user is accessible via "Profile" property from within an .aspx page. Unfortunately, this option is not available if you want to access profiles from another project, so creating a custom profile base class is a safe bet anyway.