1
votes

My application is an MVC4 application with a Domain Model created in EF 5 Code First. The application requires Authentication / Authorization, and I want to use the default ASP.NET Membership Provider.

With this in mind, I have gone ahead and used the aspnet_reqsql utility to add all the tables necessary for the ASP.NET Default Membership provider.

However, my application needs to store more information about the User than what is provided by default by the Membership provider. For example:

  • First Name
  • Last Name
  • Date of Birth
  • Address (split into different columns)

These things are not present in the membership provider tables. So I went ahead and added all the missing columns to the users table, and also created an Addresses table, and created a relationship between the User and the Address.

I then went into my Registration View Model, and added the missing data fields, I then went into the AccountController and checked the method that gets called to register a user. It is this:

    //
    // Summary:
    //     Creates a new user profile entry and a new membership account.
    //
    // Parameters:
    //   userName:
    //     The user name.
    //
    //   password:
    //     The password for the user.
    //
    //   propertyValues:
    //     (Optional) A dictionary that contains additional user attributes. The default
    //     is null.
    //
    //   requireConfirmationToken:
    //     (Optional) true to specify that the user account must be confirmed; otherwise,
    //     false. The default is false.
    //
    // Returns:
    //     A token that can be sent to the user to confirm the user account.
    //
    // Exceptions:
    //   System.InvalidOperationException:
    //     The WebMatrix.WebData.SimpleMembershipProvider.Initialize(System.String,System.Collections.Specialized.NameValueCollection)
    //     method was not called.-or-The Overload:WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection
    //     method was not called.-or-The WebMatrix.WebData.SimpleMembershipProvider
    //     membership provider is not registered in the configuration of your site.
    //     For more information, contact your site's system administrator.
    public static string CreateUserAndAccount(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false);

This method is confusing me a lot ! I was expecting to see the logic of data insertion into the database, so that I may edit it and add make the method take care of my newly added fields too, but all that missing!

What am I missing? How can I achieve the type of registration that I want?

1

1 Answers

2
votes
  1. First of all, you want to use new ASP.NET Universal Providers which uses Entity Framework.
  2. If you want to add custom columns, create a new table like the following, and retrieves that custom data based on UserId by yourself.

Note: You cannot alter (add/remove) columns of any table created by Membership Provider, if you want to use DefaultMembershipProvider.

In other words, if you start adding columns, you'll have to implement CustomMembersipProvider. I do not recommend it if you are new to MembershipProvider.

enter image description here