ShowUsYour<Blog>

Irregular expressions regularly

Speculating about Membership, RoleProvider and ProfileAPI implementation

There's an important area in ASP.NET Whidbey that I've started looking into that I haven't seen much coverage on, that is, how will the new user/security features be used when building a real application?

Membership, RoleProvider and ProfileAPI are the 3 that I'm talking about here.

Membership has some great features for managing users such as creating, retrieving, updating and deleting user  There's also a MembershipUser class which contains some of the instance data for users such as username, email, etc.

The ProfileAPI allows you to store additional arbitrary pieces of data against a MembershipUser.

So, from this you can see that there is a disjoint between all of the user information with some of it stored in the MembershipUser class and the remainder (aside from roles) stored in the Profile class.

Writing a wrapper around Membership, RoleProvider and ProfileAPI

In doing some speculative thinking over the weekend to try and envisage how these classes will get used, I imagined that you would need to create a "User" class to unify that data.  For example, I'd imagine that you wouldn't want to be passing System.Web.Membership.MembershipUser's out of your web services.

So, in-line with the starter kit architecture of today, I would imagine that some sort of User and UserBL classes may get written to unify access to User data.

class UserBL {

    public static bool ValidateUser(MyApplicationUser u) { return Membership.ValidateUser( ... ) }

    public static MyApplicationUser GetUser(string username) { 

        MembershipUser tmp = Membership.GetUser( ... )
        MyApplicationUser usr = new MyApplicationUser()

        // read in all values from MembershipUser
        usr.prop = tmp.prop

        // add values from Profile
        usr.prop = Profile.prop
        

        return usr
    }

    ... etc

}
Posted: Jan 31 2005, 08:05 AM by digory | with 4 comment(s)
Filed under:

Comments

TrackBack said:

# January 30, 2005 3:15 PM

scottgu said:

Hi Darren,

You probably do not want to have a User class like the one above. The reason being that every time you call Membership.GetUser() you are making a request to the database to gather membership information about the user. In 90% of all cases this is un-necessary -- since you can easily get the current username by using the User.Identity.Name property. This is much, much, more efficient than hitting the database.

Instead, for storing ambient properties about a user you should just use the Profile object directly.

Hope this helps,

Scott
# January 30, 2005 5:10 PM

Darren Neimke said:

Thanks Scott... just working through this stuff. I hear what you are saying. In my example I should have had some caching in there:

{PSUEDO-CODE}

public static MyApplicationUser GetUser(string username) {
MyApplicationUser usr = Cache.Get( username ) ;

if( usr == null ) {

MembershipUser tmp = Membership.GetUser( ... )
MyApplicationUser usr = new MyApplicationUser()

// read in all values from MembershipUser
usr.prop = tmp.prop

// add values from Profile
usr.prop = Profile.prop
}

return usr
}
# January 30, 2005 7:32 PM

Darren Neimke said:

... forgot to mention that, in the case where I needed just the User.Identity.Name I'd probably reference just that.

In ProjectDistributor for example, I've wrapped that property inside of a Globals accessor, i.e.:

Globals.CurrentUserName
# January 30, 2005 7:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)