views:

357

answers:

2

Hi.

I have recently installed the aspnetdb and have been customizing it for the purposes of my application design. I have extended the ASPNET providers as part of my class design: MyMembershipProvider, MyProfileProvider, and MyRoleProvider. Everything works well there.

Now, per the requierments of my system, I need to add custom data and I don't want to use the name / value design provided in aspnet_Profile. So, I have created two custom tables:

dbo.Profile

CREATE TABLE [dbo].[Profiles](
    [profileid] [int] IDENTITY(1,1) NOT NULL,
    [userid] [uniqueidentifier] NOT NULL,
    [username] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [applicationname] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [confirmcode] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [isanonymous] [bit] NULL,
    [lastactivity] [datetime] NULL,
    [lastupdated] [datetime] NULL,
 CONSTRAINT [PK__Profiles__1DB06A4F] PRIMARY KEY CLUSTERED 
(
    [profileid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [PKProfiles] UNIQUE NONCLUSTERED 
(
    [username] ASC,
    [applicationname] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

and

dbo.ProfileData

CREATE TABLE [dbo].[ProfileData](
    [profiledataid] [int] IDENTITY(1,1) NOT NULL,
    [profileid] [int] NOT NULL,
    [lastname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [firstname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [alternateemail] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [zipcode] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [birthmonth] [tinyint] NULL,
    [birthday] [tinyint] NULL,
    [birthyear] [int] NULL,
    [gender] [varchar](10) COLLATE Latin1_General_CI_AI NULL,
    [city] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [state] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [country] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [ipaddress] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [sessionid] [bigint] NULL,
 CONSTRAINT [PK_ProfileData] PRIMARY KEY CLUSTERED 
(
    [profiledataid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Now, since I have custom data extending the ASPNET infrastructure, I have written a custom user class that helps extend that data as part of the profile snapshot. I basically followed John Galloway's suggestions in this blog

public class CustomUser : ProfileBase
{

        public customuser() {}

         public static CustomUser GetCustomUserProfile(string username)
        {
            return System.Web.Profile.ProfileBase.Create(username) as CustomUser;
        }

        #region ProfileBase Extended Properties

        [SettingsAllowAnonymous(false), CustomProviderData("FirstName;string")]
        public string FirstName { get { return base["FirstName"] as string; } set { base["FirstName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("LastName;string")]
        public string LastName { get { return base["LastName"] as string; } set { base["LastName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("AlternateEmail;string")]
        public string AlternateEmail { get { return base["AlternateEmail"] as string; } set { base["AlternateEmail"] = value; } }

       // AND SO ON...

        #endregion

}

OK, so far so good. I have a CustomUser class that I can use for getting and setting profile data as such:

CustomUser _cu = CustomUser.GetUserProfile(username);

This will return all my custom fields in addition to the properties inherited from ProfileBase. Great.

But what if I want to add other properties to the User's profile? Namely, those provided by ASPNET's MembershipUser class such as IsOnline, PasswordQuestion, IsLockedOut, etc... To get base properties from MembershipUser, I could try something like:

public class CustomUser : MembershipUser
    {
        public CustomUser(string providerName, string name, object providerUserKey, string email,
                                string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
                                DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
                                DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
            : base(
                providerName, name, providerUserKey, email, passwordQuestion, comment, isApproved, isLockedOut,
                creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
        {
        }

        protected CustomUser()
        {
        }

        // e.g. no desire to use Profile, can just add data
        // say, from a flat record containing all user data
        public string MyCustomField { get; set; }
    }

But, because I have also been inheriting from ProfileBase and C# does not allow multiple inheritance (ie., I can't do CustomUser: ProfileBase, MembershipUser), I am stuck with a bit of a problem. What is the best way to architect my user class so User _user = new User(); returns the full breadth of properties relevant to a authenticated user?

Below is my first stab at this "SuperUser" class but I am having a real hard time figuring out how to create a new instance of the class object such that both CustomUser and MembershipUser are created.

Am I going about this the right way?

    public class User
    {

        public User() { }

        public static User GetUserProfile(string username)
        {
             // return combined profile based on username
        }

        #region CustomUser Members

        private CustomUser _customUser
        {
            get
            {       
                if (UserName != null)
                {
                    try
                    {
                        return ProfileBase.Create(UserName) as CustomUser;
                    }
                    catch { return null;  }
                }
                else
                {
                    try 
                    {
                        // this will work if the site user is log'd in
                        return ProfileBase.Create(Membership.GetUser().UserName) as CustomUser;
                    }
                    catch { return null;  }
                }

            }
        }

        public string FirstName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.FirstName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.FirstName = value;
                }
            }
        }

        public string LastName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.LastName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.LastName = value;
                }
            }
        }


        #endregion

        #region MembershipUser Members

        //corresponding MembershipUser
        private MembershipUser _membershipUser
        {
            get
            {
                if (UserName != null)
                {
                    return Membership.GetUser(UserName);
                }
                return null;
            }
        }

        //Properties looked up from MembershipUser

        public string UserName
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.UserName;
                }
                return string.Empty;
            }

        }

        public string Email
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.Email;
                }
                return string.Empty;
            }

        }

        public object ProviderUserKey
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.ProviderUserKey;
                }
                return null;
            }
        }


        #endregion

     }

}
+1  A: 

You'll have to implement your own Custom Membership Provider to populate it as well.

Bryce Fischer
Ah, I think I just realized my problem (which you pointed out but it took a while... :) ). My CustomUser class extends ProfileBase and I need to write a CustomMembershipUser class which extends MembershipUser. Then, I am guessing I should be able to combine the two in my User class in the same way that I have done at the end of my post?
Code Sherpa
I think Sky Sanders above provides a better explanation than I did. Take a look at the link he provided. Its very easy to create a custom MembershipProvider, its just kind of tedious as there are a lot of method/properties you need to override.That way, to get your User object, you'd do something like:CustomUser user = (CustomUser)Membership.GetUser();
Bryce Fischer
@bryce - actually there are only a few methods, 2 i believe, that you need to get a skeleton membership provider up and running, ValidateUser() and GetUser(userName,isUserOnline). The rest you implement to support the framework features you wish to leverage. It really couldn't be easier.
Sky Sanders
+1  A: 

Code, It looks like you are planning to do a lot of unnecessary work. And there is no compelling reason to aggregate the membership and profile infrastructure in a wrapper class and many reasons not to.

The first reason is that you can't use it anywhere. You will have to build your own infrastructure. I have done it and it aint fun.

What is fun is coding to an established api and taking advantage of the millions of dollars and thousands of man hours put into designing, developing and testing the provider based systems of ASP.NET.

If you wish to have a custom membership user the first step is to inherit from MembershipUser so that you can easily implement a custom provider that plugs right into the asp.net framework.

See here for a very quick and easily digested example and here for the standard docs.

As far as composing profile within your user. Membership, Roles and Profile are separate but related facets of the infrastructure and have been logically separated to maximize flexibility and reuse.

I am certain that you will find more value in learning to code to the provider based api and focusing on solving interesting problems than re-inventing the wheel.

Good luck

Sky Sanders
Thanks a ton Sky, that was helpful. I have seriously edited my question to provide a full background on my thinking. Per your response, I guess my question is - even if I inherit from MembershipUser, how do I account for the properties that I want to expose via ProfileBase? I am not trying to reinvent the wheel, but I would like to be able to provide a complete snapshot of the user by combining them.
Code Sherpa
@Code Sherpa, You dont need to combine membership and profile, they are both available at all times, anywhere via httpcontext.current. The fluid nature of the profile system does not make it a good candidate for baking into you membership. If you have some datapoints that need to follow the membership user around simply add them to the membershipuser/database. Do not pull from profile to populate membership. I don't even have time to explain how bad of an idea that is. lol. trust me. I have dealt with the provider model from the metal up.
Sky Sanders
Thanks Again Sky. I'll go ahead and follow the example you provided. As a parting question, would you mind taking a look at John Galloway's blog post referenced in my edited post? I have been following his example which creates a custom user profile by inheriting from ProfileBase. As mentioned above, you advise to inherit from MembershipUser and not bake profile properties into my custom user class. So, is John's advice simply wrong? How should I be accessing ProfileBase? Thanks again.
Code Sherpa
@Code Sherpa, As I said, Profile and Membership are distinct. They are not the same. I don't believe John mentions membership anywhere in the article, except to get the current user's username in order to initialize the profilebase, so i am not sure where you got the idea that merging membership and profile was a good idea. I don't think it was from that post. The post is good and provides good guidance on what might otherwise be a slightly confusing process, r.e. profile in web apps vs. web sites.
Sky Sanders
OK, thanks Sky. I am new to the ASPNET DB and learning my way around. I must have confused membership and profiles at some point. Thanks for setting that straight. I'll follow the example you provided. Thanks again for your help.
Code Sherpa