Add custom fields to MVC4 simple membership




ASP.net MVC 4 there is shipped with Membership Provider and a Simple Membership Provider to enable quick and easy site login functionality for the ASP.net MVC website. in this post I will discuss how to add additional custom fields to the MVC4 simple membership provider.

During the course of developing your website, you may come across a requirement where you may need to add a little more than the Basic data to the User table on registration.

To add additional custom data to the User Table and populate consists of 5 steps, this is based on the assumption that you have already started a new ASP.net 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)

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.

Additional resources

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

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines. A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

  • fguigui

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

    • 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

        • 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.

    • 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!

    • Thanks for the feedback! Glad it helped, my goal achieved of helping at least one person 🙂

  • kingkong balinggao

    Thank you very much for this. 🙂

  • abou

    hi

    i try to custom field to my project MVC4 ( VS2013) but i don’t have RegisterModel.cs !!

    • Hi, This article was written based on the MVC template shipped with MVC4 in visual studio 2012. I think the templates that shipped with VS 2013 are really different and I also think the membership providers also changed a bit.

      I haven’t done any work with those at this stage,

  • alex

    it works

π