private void btnLogin_Click(object sender, EventArgs e) { try { //如果使用帳號登入 if (true || rbAccount.Checked) { //string username = tbxUsername.Text.Trim(); string username = this.LoginUserName; string password = tbxPassword.Text.Trim(); //檢查是否為空 if (username != string.Empty && password != string.Empty) { Regex regex = new Regex("^[A-Za-z0-9]+$"); //檢查是否包含非法字元 if (regex.IsMatch(username) && regex.IsMatch(password)) { _loginUser = User.GetUser(username, password); User.CurrentUser = _loginUser; this.DialogResult = DialogResult.OK; } else { throw new SWLHMSException("帳號或密碼含有非法字元,請檢查後重新輸入"); } } else { throw new SWLHMSException("帳號及密碼不得為空"); } } else { _loginUser = User.GetUser("guest", "null"); this.DialogResult = DialogResult.OK; } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); MessageBox.Show("登入時發生錯誤\n" + ex.Message); } }
public static bool ChangePassword(string username, string oriPassword, string newPassword) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^[A-Za-z0-9]+$"); User user = User.GetUser(username, oriPassword); if (oriPassword == user.密碼 && regex.IsMatch(newPassword)) { user.DataRow.密碼 = newPassword; user.Save(); return(true); } else { return(false); } }