예제 #1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    //WebSecurity.CreateUserAndAccount(model.UserName, model.Password); //Replaced with below
                    using (MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["MySqlConnString"].ConnectionString))
                    {
                        if (connection.State != System.Data.ConnectionState.Open)
                            connection.Open();
                        byte[] passwordInBytes = System.Text.Encoding.ASCII.GetBytes(model.Password); //Convert password to byte array
                        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); //Initialize md5 algorithm
                        passwordInBytes = md5.ComputeHash(passwordInBytes); //Compute hash value
                        string encodedPassword = BitConverter.ToString(passwordInBytes).Replace("-",""); //NOTE: Formats hash value as a string of characters without dashes b/c initially came with dashes
                        MySqlCommand command = new MySqlCommand("INSERT INTO person (password, fname, lname)" +
                                                                "VALUES ('" + encodedPassword + "', '" + model.FirstName + "', '" + model.LastName + "');", connection);
                        command.ExecuteNonQuery(); //Only for GET BY ID, DELETE, UPDATE, and INSERT statements -> returning a single row/tuple
                        connection.Close();//Added close because it was always open
                    }
                    //WebSecurity.Login(model.FirstName, model.Password);
                    LoginModel user = new LoginModel(); //Convert from RegisterModel to LoginModel
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.Password = model.Password;
                    Login(user, ViewBag.ReturnUrl); //Jump to login after registering
                    return RedirectToAction("Index", "Home"); //CASE 1: Return to Home page
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
예제 #2
0
        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);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

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