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

 [Table("UserProfiles")]
    public class UserProfiles
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string Email { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string MobileNumber { get; set; }
     }

 Step 2

Add the additional properties to your RegisterModel.cs

public class RegisterModel
    {
        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [StringLength(11, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 11)]
        [Display(Name = "Mobile No.")]
        public string MobileNumber { get; set; }

    }





Step 3

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

@using (Html.BeginForm()) {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary()
  @Html.LabelFor(m = m.Email)
  @Html.TextBoxFor(m => m.Email)
  @Html.LabelFor(m => m.Password)
  @Html.PasswordFor(m => m.Password)
  @Html.LabelFor(m => m.FirstName)
  @Html.TextBoxFor(m => m.FirstName)
  @Html.LabelFor(m => m.LastName)
  @Html.TextBoxFor(m => m.LastName)
  @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!

 [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.Email, model.Password,
                                                     new
                                                         {
                                                             FirstName = model.FirstName,
                                                             LastName = model.LastName,
                                                             MobileNumber = model.MobileNumber
                                                         });
                    WebSecurity.Login(model.Email, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }




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

Freelance Full Stack Developer at threenine.co.uk
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.

Latest posts by Gary Woodfine (see all)