示例#1
0
        //Add new User
        public static bool AddNewUserRegistration(string email, string password)
        {
            bool process_status = false;

            using (var db = new DBConnection())
            {
                string   salt            = AppFunctions.RandomString(GetRandomSaltLength());
                string   hashed_password = ReturnHashPassword(password, email, salt);
                string[] directory_arr   = email.Split(new[] { '@' });
                string   directory_name  = directory_arr[0];
                //If name already exist, add random number to name
                if (db.Accounts.Any(s => s.DirectoryName == directory_name))
                {
                    directory_name = directory_name + AppFunctions.RandomInt(4);
                }
                AccountsModel AccountData = new AccountsModel
                {
                    Email               = email,
                    Password            = hashed_password,
                    Salt                = salt,
                    Status              = 1,
                    AccountVerification = 0,
                    DirectoryName       = directory_name,
                    DateCreated         = DateTime.Now
                                          // …
                };

                db.Accounts.Add(AccountData);

                try
                {
                    db.SaveChanges();
                    process_status = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    //Log Error
                    LogError(ex, null, "AddNewUserRegistration", null);
                }
            }
            return(process_status);
        }
示例#2
0
        //Add New Registration
        public static bool AddNewRegistration(string email, string password, bool external)
        {
            bool process_status = false;

            int oauth            = 0;
            int account_verified = 0;

            if (external)
            {
                password         = ShuffleString(password);
                oauth            = 1;
                account_verified = 1;
            }

            string salt            = AppFunctions.RandomString(GetRandomSaltLength());
            string hashed_password = ReturnHashPassword(password, email, salt);
            string directory_name  = AppFunctions.GetUsernameFromEmail(email);

            //If name already exist, add random number to name
            using (var db = new DBConnection())
            {
                if (db.Accounts.Any(s => s.DirectoryName == directory_name))
                {
                    directory_name = directory_name + AppFunctions.RandomInt(4);
                }
            }

            //Create directory
            //System.IO.Directory.CreateDirectory("~/images/account");

            string        connString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
            SqlConnection conn       = null;

            try
            {
                conn = new SqlConnection(connString);
                conn.Open();

                using (SqlCommand cmd = new SqlCommand())
                {
                    //Insert record to Users db
                    cmd.Connection  = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = @"INSERT INTO Accounts ([Email], [Password], [Salt], [Status], [Oauth], [AccountVerification], [DirectoryName], [DateCreated]) 
                                            VALUES 
                                    (@var0, @var1, @var2, @var3, @var4, @var5, @var6, @var7)";
                    cmd.Parameters.AddWithValue("@var0", email);
                    cmd.Parameters.AddWithValue("@var1", hashed_password);
                    cmd.Parameters.AddWithValue("@var2", salt);
                    cmd.Parameters.AddWithValue("@var3", 1);
                    cmd.Parameters.AddWithValue("@var4", oauth);
                    cmd.Parameters.AddWithValue("@var5", account_verified);
                    cmd.Parameters.AddWithValue("@var6", directory_name);
                    cmd.Parameters.AddWithValue("@var7", DateTime.Now);
                    int rowsAffected = cmd.ExecuteNonQuery();
                    if (rowsAffected == 1)
                    {
                        process_status = true;
                    }
                    else
                    {
                        process_status = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error! <br>" + ex);
                //Log Error
                LogError(ex, null, "AddNewRegistration", null);
            }
            finally
            {
                if (conn != null)
                {
                    //cleanup connection i.e close
                    conn.Close();
                }
            }
            return(process_status);
        }