コード例 #1
0
        [ValidateAntiForgeryToken] //this prevents cross site scripting
        public ActionResult Register(User userModel)
        {
            if (ModelState.IsValid)
            {
                using (StudentBookAppDB dbModel = new StudentBookAppDB())
                {
                    if (dbModel.Users.Any(x => x.EmailID == userModel.EmailID))                    //this checks if the user is using an email address the same as another user in the user database
                    {
                        ViewBag.DuplicateMessage = "E-Mail Already Exists, Please Choose another"; //this displays the error message to the user
                        return(View("Register", userModel));                                       //this returns the view for the register page
                    }

                    userModel.Password        = Models.Extended.Crypto.getHash(userModel.Password);        //this converts the password and passe's it to the Encrpt Class to convert the password
                    userModel.ConfirmPassword = Models.Extended.Crypto.getHash(userModel.ConfirmPassword); //this also hashe's the confirmed password
                    dbModel.Users.Add(userModel);                                                          //this adds the user to the database when they register for an account
                    dbModel.SaveChanges();                                                                 //this saves the changes in the database
                }
                ModelState.Clear();
                ViewBag.SuccessMessage = "Registration has been successful for " + userModel.EmailID; //this lets the user know that the registration for their account has been successful
            }
            return(View("Register", new User()));                                                     //this returns to the view with the new user object
        }