示例#1
0
        /* CreateUser takes the employees first and last name and desired pw.
         *
         * will automatically generate a unique username (first initial, last name + numbers as needed)
         *
         * will generate a salt for the pw, and store the salt and the resulting hash in the database, along with the firstname/lastname
         * username. Additional information will need to be stored by other means
         *
         * returns the generated username as a string.
         *
         *
         * if this errors out in some way, it will should return null - check when using that username !null.
         */

        public static string CreateUser(string fName, string lName, string pw)
        {
            // outputs the date in format equal to the rest of the table
            var today = DateTime.Today.ToString("yyyy-MM-dd");

            // automatically create the username as first initial, last name - all lowercase
            string username = fName[0] + lName;

            username = username.ToLower();

            /* if there happen to be 2 people with the same first initial/last name combo
             * then this section will add a number to the end of the username.
             */

            int i = 1;

            while (CheckUsername(username))
            {
                if (i > 1 && i < 10)
                {
                    /* if there happen to be more than 2 people with the same first initial, last name
                     * then we remove the 1 (the last char fo the string) and add the new incrimimented i
                     * to the username (so username2, then username3, ect)
                     */
                    username = username.Substring(0, username.Length - 1);
                }
                else if (i >= 10)
                {
                    /* Let's be real here. If there are more than 10 people with the exact same first
                     * initial and last name, there there is either nepotism or something very weird going on
                     * but just in case, we're removing 2 numbers if it gets above 10 for i.
                     *
                     * we're not going to check for 3 numbers. Something is messed up, contact IT
                     */
                    username = username.Substring(0, username.Length - 2);
                }
                username += i; // add the iteration number (starting at 1!!!) to the end of the preset username.
                i++;
            }

            //Generate the salt and the hash
            var passwordSalt = PasswordHash.GenerateSalt();
            var passwordHash = PasswordHash.ComputeHash(pw, passwordSalt);

            //convert the hash and salt to a string for database storage
            string saltString = Convert.ToBase64String(passwordSalt);
            string hashString = Convert.ToBase64String(passwordHash);

            //setup the sql string for insertion

            string sql = "INSERT INTO EMPLOYEE (first_name, last_name, username, password, salt, hired) VALUES ('" + fName + "','" + lName + "','" + username + "','" + hashString + "','" + saltString + "','" + today + "')";

            if (CcnSession.SendQry(new MySqlCommand(sql)))
            {
                return(username);
            }
            else
            {
                return(null);
            }
        }