Add custom fields to MVC4 simple membership

In MVC 4  there is a new Membership Provider, SimpleMembership Provider, with which I have been playing around with quite a bit for a new project I am working on.  There has been a lot of discussions and debate regarding the SimpleMembership Provider and it seems that the community at large, does not like it. However, I am of the view that if it is going to save me sometime, and provide me with a quick and easy way to implement a standard website login facility then I’ll use it, knowing that I can quite easily fit in another at a later stage.    However, that being said I did come across a particular situation today where I found that the SimpleMembership provider didn’t quite live up to it’s name,  and something just wasn’t quite intuitive enough at first.

The situation was that I needed to add a little more than the Basic data to the User table on registration. I couldn’t quite figure it out at first, and I found the documentation to be a little lacking (at the time writing).  I’ll attempt to fill that gap with this post.

To add custom data to the User Table,  in 5 steps, this is based on the assumption that you have already started a new MVC 4 project using the “Internet Template”.

Step 1

In your model folder, there is an AccountModel.cs, open it, find the UserProfile class and make the following changes

 Step 2

Add the additional properties to your RegisterModel.cs

Step 3

Display the Additonal Fields on you Regsiter View. ( ../Views/Account/Register.cshtml)

Registration Form
  1. @Html.LabelFor(m = m.Email)
    @Html.TextBoxFor(m => m.Email)
  2. @Html.LabelFor(m => m.Password)
    @Html.PasswordFor(m => m.Password)
  3. @Html.LabelFor(m => m.FirstName)
    @Html.TextBoxFor(m => m.FirstName)
  4. @Html.LabelFor(m => m.LastName)
    @Html.TextBoxFor(m => m.LastName)
  5. @Html.LabelFor(m => m.MobileNumber)
    @Html.TextBoxFor(m => m.MobileNumber)



Step 4

We’ll need to ammend the AccountController.cs with the following code.  This is where I found it a little tricky as it it just didn’t feel right at this point, the fact that you just simply create a new object and pump data into it!

This is all you need to do. Pretty cool, and quite scalable.

In learning to use the SimpleMembership Provider I found that the following tow blog posts gave me all the information I needed.

Shout it

Tagged with:
Posted in MVC, C#, Programming, Tips & Tricks, Web Development
  • fguigui

    And is it possible to add tables (simply) to the membership ?

    • Gary Woodfine

      Apologies for the delay. For some reason I wasn’t notified of comments.

      I don’t understand your question? In what way do you want to add tables the membership?

      • fguigui

        because sometime your application is just a management of users (membership) for which you just have to add some tables. Everything is then in the database membership.
        I’ve experienced it was difficult (impossible ?) to access these added tables by entity-framework access with a different connectionstring of the connectionstring of the simplemembership;
        Then that simplemembershipprovider can’t help if you want to access added tables to membership database

        • Gary Woodfine

          I can say that I have not experienced that problem. I have created a database schema and used the SimpleMembership provider to access only the required membership tables, but have still used the same connection string to access other tables in the Schema for the information and functionality.

          Granted I couldn’t say access the “Company” table by using the SimpleMembership provider, but that is OK, because the SimpleMembership provider is not used to access that, it is purely used for Access and Simple Account Management. It’s up to you to implement the more complex functionality by using your own implementation.

          • fguigui

            I ‘ll add, sometime users data and applications data are too intricated to be in 2 differents db.
            You perhaps know aspnet_regsql (tool to create a membership database), it ask you if you want a separate db or a common db for your membership and your application.

  • René López

    Hey thank you so much for this awesome post, I’m just starting to grasp MVC 4 and simple membership and I was asking myself how can I add more fields to the default Register Model, so now that I found your post, you explain it in 5 easy steps, thanks a lot, I’ll make sure to fallow your future posts.

    • Gary Woodfine

      Thanks! I need to get back into the habit of posting again :-)

  • jjspierx

    Thanks so much! Your steps made it easy and clear for me to add additional user information on registration! Much appreciated!

    • Gary Woodfine

      Thanks for the feedback! Glad it helped, my goal achieved of helping at least one person :-)

Follow me on Twitter

%d bloggers like this: