0

Okay so I have a MVC project that auto generates an AccountController AcountModel and the associated views.
I created a database with 3 tables using the model first approach, and generated all the controllers/views for all the CRUD operations.

The database contains a user table with a user id, email and password.

How can I use this user table with the auto generated AccountController for user login and registration?

2
  • There is no way to auto generate AccountController, just for CRUD operations. You have to implement by yourself. Commented Dec 13, 2012 at 15:59
  • what do you mean by auto generate? Commented Dec 13, 2012 at 16:03

2 Answers 2

1

I will show you registration process only , refering which you can build your login/registration with custom database.

Models: You will add your custommodel to the AccountModels.cs, So it will have following details:

 public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

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

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

    public class LogOnModel
    {

        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }
    }

    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Email address")]
        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; }

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


    public class userDetailModel
    {
        [Key]
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string city { get; set; }
        public string ConfirmPassword { get; set; }
        public string comapny { get; set; }
        public int zip { get; set; }
    }

Context: You will add custom context to the Models as below:

public class userDetailsDBContext: DbContext
    {
        public DbSet<userDetailModel> details { get; set; }

    }

Controller: Now we will modify our AccountController for registration as below:

public class AccountController : Controller
    {
        private userDetailsDBContext db = new userDetailsDBContext();
        // POST: /Account/Register

        [HttpPost]
        public ActionResult Register(userDetailModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    var newuser = Membership.GetUser(model.UserName);
                    model.UserId =(Guid)newuser.ProviderUserKey;
                    db.details.Add(model);
                    db.SaveChanges();
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

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

        } 
}

EDIT web.config: Finally, you will have to add the new context to the connectionstrings as below:

<connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MembershipSample-20121105163515;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MembershipSample-20121105163515.mdf" />
    <add name="userDetailsDBContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MembershipSample-20121105163515;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MembershipSample-20121105163515.mdf" />
  </connectionStrings>

You can change the database name to whatever you want and put it as your convenience but put the path here correctly.

Hope you have got the idea now...

Sign up to request clarification or add additional context in comments.

1 Comment

You will have to modify the AccountController a bit like I have done to work for your own custom tables and database...
0

I think waht you need is to create custom membership provider. This code project article will give you aplace to start.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.