开发者

Trying to create 'SuperUser Class' with ASPNET ProfileBase and MembershipUser

开发者 https://www.devze.com 2022-12-20 10:17 出处:网络
I have recently installed the aspnetdb and have been customizing it for the purposes of my application design. I have extended the ASPNET

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 = Custom开发者_Go百科User.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

     }

}


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号