Exemplo n.º 1
0
        private void Login_Click(object sender, EventArgs e)
        {
            User = new Profile();
            string folderName    = System.IO.Directory.GetCurrentDirectory();
            string subFolder1    = LoginName.Text + LoginPassword.Text;
            string pathString    = System.IO.Path.Combine(folderName, subFolder1);
            string fileName      = LoginName.Text + ".txt";
            string newPathString = System.IO.Path.Combine(pathString, fileName);

            if (System.IO.Directory.Exists(pathString))
            {
                using (StreamReader reader = new StreamReader(newPathString))
                {
                    User.ProfileName = reader.ReadLine();
                    User.Password    = reader.ReadLine();
                    User.UserName    = reader.ReadLine();
                }
                MyApp.SelectedIndex = 3;
                LoginName.Clear();
                LoginPassword.Clear();
            }
            else
            {
                DialogResult result = MessageBox.Show("This Account Does Not Exist, Do You Wish To Signup?", "Login error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (result == DialogResult.Yes)
                {
                    MyApp.SelectedIndex = 2;
                }
                LoginName.Clear();
                LoginPassword.Clear();
            }
        }
        private void Login_Click(object sender, EventArgs e)
        {
            string userName = LoginUserName.Text;
            string password = LoginPassword.Text;


            try
            {
                if (Type.Text == "" || Type.Text == null)
                {
                    MessageBox.Show("请选择用户身份");
                }
                else if (LoginUserName.Text == "" || LoginUserName.Text == null || LoginPassword.Text == "" || LoginPassword.Text == "")
                {
                    MessageBox.Show("请输入正确的用户名和密码");
                }
                else if (Type.Text == "管理员")
                {
                    string SelectSQL = "select * from admin where UserName=N'" + userName + "'";

                    DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, SelectSQL);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //该用户名存在,从数据中获取保存的加盐哈希值,赋值给变量goodhash
                        string goodhash = ds.Tables[0].Rows[0]["Password"].ToString();

                        //判断用户输入的密码是否匹配
                        if (PasswordSecurity.PasswordStorage.VerifyPassword(password, goodhash))
                        {
                            f9 = new Form9();
                            f9.Show();
                            this.Hide();
                        }
                        else
                        {
                            LoginUserName.Clear();
                            LoginPassword.Clear();
                            MessageBox.Show("如果不是管理员,请离开");
                        }
                    }
                    else
                    {
                        LoginUserName.Clear();
                        LoginPassword.Clear();
                        MessageBox.Show("如果不是管理员,请离开");
                    }
                }
                else if (Type.Text == "教师")
                {
                    string SelectSQL = "select * from teacher where UserName=N'" + userName + "'";

                    DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, SelectSQL);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        //该用户名存在,从数据中获取保存的加盐哈希值,赋值给变量goodhash
                        string goodhash = ds.Tables[0].Rows[0]["Password"].ToString();
                        //判断用户输入的密码是否匹配
                        if (PasswordSecurity.PasswordStorage.VerifyPassword(password, goodhash))
                        {
                            f3 = new Form3();
                            f3.Show();
                            this.Hide();
                        }
                        else
                        {
                            LoginUserName.Clear();
                            LoginPassword.Clear();
                            MessageBox.Show("如果不是老师,请选择正确的身份登录");
                        }
                    }
                    else
                    {
                        LoginUserName.Clear();
                        LoginPassword.Clear();
                        MessageBox.Show("如果不是老师,请选择正确的身份登录");
                    }
                }
                else if (Type.Text == "学生")
                {
                    string SelectSQL = "select * from StdInfo where UserName=N'" + userName + "'";

                    DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, SelectSQL);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        string goodhash = ds.Tables[0].Rows[0]["Password"].ToString();
                        if (PasswordSecurity.PasswordStorage.VerifyPassword(password, goodhash))
                        {
                            f2 = new Form2();
                            f2.Show();
                            this.Hide();
                        }
                        else
                        {
                            LoginUserName.Clear();
                            LoginPassword.Clear();
                            MessageBox.Show("用户名或密码错误");
                        }
                    }
                    else
                    {
                        LoginUserName.Clear();
                        LoginPassword.Clear();
                        MessageBox.Show("用户名或密码错误");
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码错误或身份不匹配");
                }
            }
            catch
            {
                MessageBox.Show("用户名或密码输入错误");
            }
        }