Cris
Cris

Reputation: 12194

asp.net mvc: how to extend account controller?

I have a asp.net mvc application that uses standard account controller to handle auth. The client asked me to add new fields as email, city, age to account creation. Which would be the best way to extend account controller/model? Should i modify aspnet_users table or should i create a new table with the new fields? Is it a right way to add new fields to AccountFormViewModel?

Upvotes: 1

Views: 2849

Answers (2)

MoYas
MoYas

Reputation: 1

First you have modify the RegisterModel AcountModels, and add new fileds

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { 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; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }


}

in AcountController you modify the Register action

 [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);


                WebSecurity.Login(model.UserName, model.Password);

                using (var context = new ECDB())
                {
                    var username = model.UserName;
                    var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
                    user.FirstName = model.FirstName;
                    user.LastName =  model.LastName;
                    user.Email =  model.Email;
                    context.SaveChanges();
                }
                return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

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

Upvotes: 0

Iridio
Iridio

Reputation: 9271

I guess you have two option:

1) Creating your custom membership provider.

2) Using the builtin profile membership provider

Anyway you have to implement/extend the AccountController and ViewModel that you get with a new MVC project. If you choose the first option you can add your fields to the aspenet_users table. If you choose the second option you use the profile table that the framework want you to create.

If you want a "super integration" of your fields you can consider to extend the IIdentity and IPrincipal interfaces. Look at this answer for some more info.

Hope it helps

Upvotes: 1

Related Questions