Question

I always used my own code to create logins and registers but using memberships should save me a lot of time in programming these things in my following projects.

Now i've found many tutorials on implementing your own custom membership and some of them are way different than others. And i am having a hard time to understand what the best solution is for implementing a simple custom membership implementation.

For this project i just need to add some additional user information, like a streetname, postcode etc.

What i've currently made is this:

     public class CustomMembershipUser : MembershipUser
{
    public CustomMembershipUser(
        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)
    {
    }

    // Add additional properties
    public string Streetname { get; set; }
    public string Postcode { get; set; }

}

public abstract class CustomMembershipProvider : MembershipProvider
{

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        // get data logic
        var user = new CustomMembershipUser(
                    "CustomMembershipProvider",
                    "naam",
                    "1",
                    "adasd@live.com",
                    "",
                    "",
                    true,
                    false,
                    DateTime.Now,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue) { Streetname = "Test", Postcode = "Test2"};

        return user;

    }

    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer,
        bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        var user = new CustomMembershipUser(
                "CustomMembershipProvider",
                "naam",
                "1",
                "adasd@live.com",
                "",
                "",
                true,
                false,
                DateTime.Now,
                DateTime.MinValue,
                DateTime.MinValue,
                DateTime.MinValue,
                DateTime.MinValue) { Streetname = "Test", Postcode = "Test2" };
        status = MembershipCreateStatus.Success;
        return user;
    }
}

Is this the best solution for my custom membership or are there better options?

I have not tested this yet, so i have no idea if this works.

I'm using MVC 4.

Was it helpful?

Solution

First thing to know about membership is that it has nothing to do with Names, addresses, etc... It's strictly about userid's, usernames, passwords, and authentication. That's it. Don't try to extend it to add names and addresses, that's not what it's for.

Instead, you simply create your own "UserProfile" table or whatever you want to call it. This table should be keyed on the ProviderUserKey provided by the membership provider. Then you need only lookup whatever data you want in your UserProfile table without ever having to modify anything to do with Membership.

So in your case, you can use the standard membership provider, which is keyed on a GUID. Then create a table called UserProfile that has 3 columns. UserId (uniqueidentifier), StreetName, and PostalCode.

In your application, you call Membership.GetUser().ProviderUserKey and use this as the key to lookup data in your UserProfile table. This is fast, simple, and requires no custom modification to Membership.

In your AccountController.Register method, you would, after creating the membership user, also create a UserProfile record with users ProviderUserKey, and the information you want stored.

OTHER TIPS

I suggest using ASP.NET Identity: http://www.asp.net/identity

To add additional data on-top of what IdentityUser collects is relatively easy. Here is the default User Table:

  • Id
  • UserName
  • PasswordHash
  • SecurityStamp

To add data on-top of this, you simply inherit from IdentityUser:

public class AppUser : IdentityUser
{
    // Add additional properties
    public string Streetname { get; set; }
    public string Postcode { get; set; }
}

You can add additional fields as you need, but the MembershipProvider is, as far as I understand it, an outdated system to handle authentication, where-as ASP.NET Identity is much more flexible and open.

I would also suggest offsetting all the extra fields to a Profile class - just something I've found to be much better - to fill up a Profile Table instead of the User Table with all these various fields - but I believe that comes down to personal preference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top