0

好的,所以我有一个MVC project自动生成一个AccountController AcountModel和相关的视图。
我使用模型优先方法创建了一个包含 3 个表的数据库,并controllers/views为所有CRUD操作生成了所有数据。

该数据库包含一个用户表,其中包含一个id用户emailpassword

如何将此用户表与自动生成AccountController的用户登录和注册一起使用?

4

2 回答 2

1

我将仅向您展示注册过程,参考您可以使用自定义数据库构建登录/注册的过程。

模型: 您将自定义模型添加到 中AccountModels.cs,因此它将具有以下详细信息:

 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; }
    }

上下文: 您将向Models以下内容添加自定义上下文:

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

    }

控制器: 现在我们将修改我们AccountController的注册如下:

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);

        } 
}

编辑 web.config: 最后,您必须将新上下文添加到连接字符串,如下所示:

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

您可以将数据库名称更改为您想要的任何名称,并将其放置为您的方便,但将路径正确放置在此处。

希望你现在有这个想法...

于 2012-12-13T16:02:50.530 回答
0

我认为您需要创建自定义会员提供程序。这篇代码项目文章将为您提供一个起点。

于 2012-12-13T16:05:19.187 回答