Exemplo n.º 1
0
 private void btLogin_Click(object sender, EventArgs e)
 {
     if (tbUsername.Text == "" || tbPassword.Text == "")
     {
         MessageBox.Show("Vui lòng nhập tài khoản và mật khẩu");
     }
     else
     {
         MD5           mh         = MD5.Create();
         byte[]        inputBytes = System.Text.Encoding.ASCII.GetBytes(tbPassword.Text);
         byte[]        hash       = mh.ComputeHash(inputBytes);
         StringBuilder sb         = new StringBuilder();
         for (int i = 0; i < hash.Length; i++)
         {
             sb.Append(hash[i].ToString("x2"));
         }
         using (StreamReader sr = new StreamReader("account.txt"))
         {
             string line;
             int    i    = 1;
             bool   flag = false;
             while ((line = sr.ReadLine()) != null)
             {
                 if (i % 2 == 1 && line == tbUsername.Text)
                 {
                     flag = true; // co tai khoan
                     line = sr.ReadLine();
                     if (line == sb.ToString())
                     {
                         this.Hide();
                         Form formQLBan = new FormQLBan();
                         formQLBan.ShowDialog();
                         this.Close();
                     }
                     else
                     {
                         MessageBox.Show("Sai mật khẩu");
                     }
                     i++;
                 }
                 i++;
             }
             if (!flag)
             {
                 MessageBox.Show("Không tìm thấy tài khoản");
             }
         }
     }
 }
Exemplo n.º 2
0
        private void btSignup_Click(object sender, EventArgs e)
        {
            if (tbUsername.Text == "")
            {
                MessageBox.Show("Hãy nhập tài khoản");
            }
            else
            {
                if (tbPassword.Text == "")
                {
                    MessageBox.Show("Hãy nhập mật khẩu");
                }
                else
                {
                    if (tbPassword.Text != tbRepassword.Text)
                    {
                        MessageBox.Show("Mật khẩu không khớp");
                    }
                    else
                    {
                        bool flag = false;
                        if (!File.Exists("account.txt"))
                        {
                            var myFile = File.Create("account.txt");
                            myFile.Close();
                        }

                        using (StreamReader sr = new StreamReader("account.txt"))
                        {
                            string line;
                            int    i = 1;
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (i % 2 == 1 && line == tbUsername.Text)
                                {
                                    MessageBox.Show("Tài khoản đã tồn tại");
                                    flag = true;
                                    break;
                                }
                                i++;
                            }
                        }
                        if (!flag)
                        {
                            using (StreamWriter sw = new StreamWriter("account.txt", true))
                            {
                                sw.WriteLine(tbUsername.Text);
                                MD5           mh         = MD5.Create();
                                byte[]        inputBytes = System.Text.Encoding.ASCII.GetBytes(tbPassword.Text);
                                byte[]        hash       = mh.ComputeHash(inputBytes);
                                StringBuilder sb         = new StringBuilder();
                                for (int i = 0; i < hash.Length; i++)
                                {
                                    sb.Append(hash[i].ToString("x2"));
                                }

                                sw.WriteLine(sb.ToString());
                            }
                            this.Hide();
                            Form formQLBan = new FormQLBan();
                            formQLBan.ShowDialog();
                            formLogin.Close();
                        }
                    }
                }
            }
        }