Example #1
0
        private void btnAddUser_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text == "" || txtPassword.Text == "")
            {
                MessageBox.Show("Please enter all fields");
            }
            else if (txtPassword.Text == txtConfirmPassword.Text)
            {
                try
                {
                    SHA1CryptoServiceProvider sh = new SHA1CryptoServiceProvider();
                    UTF8Encoding  utf8           = new UTF8Encoding();
                    string        hash           = BitConverter.ToString(sh.ComputeHash(utf8.GetBytes(txtConfirmPassword.Text)));
                    SqlConnection con            = new SqlConnection("Data Source=.;Initial Catalog=SP;Integrated Security=True");
                    SqlCommand    cmd            = new SqlCommand("insert into Active_User(Username,pass, Access)values(@User_Name,@Password,@access)", con);

                    cmd.Parameters.AddWithValue("@User_Name", txtUsername.Text);
                    cmd.Parameters.AddWithValue("@Password", hash);
                    cmd.Parameters.AddWithValue("@access", (cbAccessLevelName.SelectedValue));


                    con.Open();
                    //var q = db.Access_L.Where(u => u.Access_Level_Name == cbAccessLevelName.Text).FirstOrDefault();
                    //int Aid = Convert.ToInt32(q.Access_Level_Id.ToString());
                    //NewUser.Access = Aid;


                    //db.Active_User.Add(NewUser);



                    string     check = @"(Select count(*) from Active_User where Username='******')";
                    SqlCommand cmda  = new SqlCommand(check, con);
                    int        count = (int)cmda.ExecuteScalar();
                    if (count > 0)
                    {
                        MessageBox.Show("A user with that username already exists");
                    }
                    else
                    {
                        try
                        {
                            //int description = 0;
                            //var AccessL = db.Access_L.Where(emp => emp.Access_Level_Name == cbAccessLevelName.Text).Select(u => u.Access_Level_Id).FirstOrDefault();
                            //description = AccessL;
                            //NewUser.Access = AccessL;
                            //NewAccess.Access_Level_Id = description;
                            //NewUser.Access = NewAccess.Access_Level_Id;

                            cmd.ExecuteNonQuery();
                            //db.Active_User.Add(NewUser);
                            //int myUser = NewUser.Active_User_Id;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("" + ex);
                        }

                        Active_User newuser = new Active_User();

                        MessageBox.Show("User successfully registered!");
                        this.Hide();
                        this.Close();
                        //Audit Log
                        int    accessidz  = newuser.Active_User_Id;
                        string user_Value = Convert.ToString(newuser);

                        Audit_Log Current_Audit6 = new Audit_Log();
                        Current_Audit6.Table_Name = "User";
                        // Current_Audit3.Users_Id = Globals.Users_Id;
                        Current_Audit6.Date_Time = DateTime.Now;
                        db.Audit_Log.Add(Current_Audit6);
                        db.SaveChanges();
                        int Log_ID6 = Current_Audit6.Audit_Log_Id;


                        Audit_Create_Delete Current_Create6 = new Audit_Create_Delete();
                        Current_Create6.Audit_Log_Id    = Log_ID6;
                        Current_Create6.Created         = true;
                        Current_Create6.PK_Row_Effected = accessidz;
                        Current_Create6.Value           = user_Value;
                        db.Audit_Create_Delete.Add(Current_Create6);
                        db.SaveChanges();
                        this.Close();
                        this.Hide();
                    }
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 2627)
                    {
                    }
                    else
                    {
                        MessageBox.Show("An Error:" + ex.Message);
                    }
                }
            }

            else if (txtPassword.Text != txtConfirmPassword.Text)
            {
                MessageBox.Show("Password and confirm Password fields do not match");
            }
        }
Example #2
0
        private void btnContinue_Click(object sender, EventArgs e)
        {
            string             Hashb;
            List <Active_User> myUser = new List <Active_User>();

            try
            {
                myUser = db.Active_User.Where(someuser => someuser.Username == txtEmail.Text).ToList();
            }
            catch (Exception)
            {
                throw;
            }
            try
            {
                Active_User emp = myUser[0];
                clsGlobals.Userlogin = myUser[0];
            }
            catch (Exception)
            {
                throw;
            }

            db.SaveChanges();
            try
            {
                SHA1CryptoServiceProvider sh = new SHA1CryptoServiceProvider();
                UTF8Encoding  utf8           = new UTF8Encoding();
                string        hash           = BitConverter.ToString(sh.ComputeHash(utf8.GetBytes(txtPassword.Text)));
                SqlConnection con            = new SqlConnection("Data Source=.;Initial Catalog=SP;Integrated Security=True");
                SqlCommand    cmd            = new SqlCommand("select pass from Active_User where Username=@Username", con);
                cmd.Parameters.AddWithValue("@Username", txtEmail.Text);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();

                Hashb = dr[0].ToString();
                con.Close();
                if (txtEmail.Text == "")
                {
                    lbWarningEmail.Visible = true;
                    //label4.Text = "Please Provide the username you wish to reset";

                    //correct = false;
                }
                if (txtPassword.Text == "")
                {
                    lbWarningPassword.Visible = true;
                    //label4.Text = "Please Provide the username you wish to reset";

                    //correct = false;
                }
                if (hash == Hashb)
                {
                    MessageBox.Show("Authorization was successful");
                    this.Dispose();
                    Users.FrmResetPassword rs = new Users.FrmResetPassword(3);
                    rs.ShowDialog();
                    rs.Focus();
                }
                else
                {
                    MessageBox.Show("Authorization failed, please try again");
                    lbWarningEmail.Visible    = true;
                    lbWarningPassword.Visible = true;
                }
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Error has occured:" + ex.Message);
            }
        }