简体   繁体   English

在MVC 3中使用自定义数据库进行用户登录/注册

[英]Using custom database in MVC 3 for user login/registration

Okay so I have a MVC project that auto generates an AccountController AcountModel and the associated views. 好的,所以我有一个MVC project ,该MVC project会自动生成一个AccountController AcountModel和关联的视图。
I created a database with 3 tables using the model first approach, and generated all the controllers/views for all the CRUD operations. 我使用模型优先方法创建了一个包含3个表的数据库,并为所有CRUD操作生成了所有controllers/views

The database contains a user table with a user id , email and password . 该数据库包含一个带有用户idemailpassword的用户表。

How can I use this user table with the auto generated AccountController for user login and registration? 如何将此用户表与自动生成的AccountController用于用户登录和注册?

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: 模型:您将自定义模型添加到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; }
    }

Context: You will add custom context to the Models as below: 上下文:您将向Models添加自定义上下文,如下所示:

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

    }

Controller: Now we will modify our AccountController for registration as below: 控制器:现在,我们将修改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);

        } 
}

EDIT web.config: Finally, you will have to add the new context to the connectionstrings as below: 编辑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>

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... 希望你现在有了主意...

I think waht you need is to create custom membership provider. 我认为您需要的是创建自定义成员资格提供程序。 This code project article will give you aplace to start. 这篇代码项目文章将为您提供一个起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVC 2中的配置文件和/或自定义用户注册 - Profiles in MVC 2 and/or custom user registration 通过存储库模式使用现有数据库的MVC4用户注册 - MVC4 user registration using existing database through repository pattern MVC5登录自定义数据库 - MVC5 Login to custom Database 如何在mvc4中进行自定义身份验证(登录,记住我和注册)? - how to make custom authentication (login, remember me and registration) in mvc4? 使用自定义筛选器MVC删除对用户的某些数据库记录的访问 - Remove access to certain Database Records to a user using Custom Filter MVC ASP.NET (6.0) Core MVC Login and Registration using Identity - ASP.NET (6.0) Core MVC Login and Registration using Identity NET MVC,EF 6:用户的数据库登录失败 - NET MVC, EF 6: database login failed for user 自定义用户登录ASP.NET MVC 4 - Custom user Login in ASP.NET MVC 4 ASP MVC登录返回用户以再次在共享成员资格数据库中登录 - ASP MVC Login is returning User to login again in shared membership database MVC.Net自定义注册/登录错误“盐不是预期的{int}。{string}格式” - MVC.Net Custom registration/login error “The salt was not in an expected format of {int}.{string}”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM