Search code examples
asp.netasp.net-mvc-3membershipuser

Custom MembershipUser with only needed parameters


I am building a custom MembershipProvider more precisely the GetUser function.

Therefor i have a custom MembershipUser.

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

In the GetUser function of the MembershipProvider i get the user data and put them into the CustomMemberShipUser.

public override MembershipUser GetUser(string email, bool userIsOnline)
    {
        User u = _db.Users.Where(x => x.Email == email).First();
        CustomMemberShipUser customUser = new CustomMemberShipUser (
                                        "CustomMemberShipUser ",
                                        u.Email, 
                                        u.id,
                                        u.Email, 
                                        "", 
                                        "", 
                                        true, 
                                        false, 
                                        u.CreateDate, 
                                        DateTime.MinValue, 
                                        DateTime.MinValue, 
                                        DateTime.MinValue, 
                                        DateTime.MinValue);
        return customUser ;
    }

As you can see i use the email as name for the MemberShip and i don't need most of the other parameters.

Is there a way to make the call simpler? I don't want to initalize the MembershipUser with empty Strings and minimal Date values.

Thanks in advance


Solution

  • Could you adapt your CustomMembershipUser to do the 'padding' for you

       public class CustomMemberShipUser : MembershipUser
        {
            public CustomMemberShipUser (
                string email,
                object providerUserKey,
                ): base(
                    "CustomMemberShipUser", email, providerUserKey, email, String.Empty,
                    String.Empty, true, false, DateTime.MinValue, DateTime.MinValue,
                    DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)
            {
            }
        }
    

    It doesn't solve the problem but it will tidy up your provider which will become

    public override MembershipUser GetUser(string email, bool userIsOnline)
        {
            User u = _db.Users.Where(x => x.Email == email).First();
            CustomMemberShipUser customUser = new CustomMemberShipUser (u.Email, u.id);
            return customUser ;
        }
    

    I presume your CustomMembershipUser is exposing some additional properties that you are not showing us. As it stands you could just return a MembershipUser. With the above the only benefit your CustomMembershipUser gives you is the cleaner construction in your CustomMembershipProvider