private void ButtonLogIn_Click(object sender, EventArgs e) { //True if all the components validate successfully if (ValidateForm()) { //Checks whether it is a student or staff member attempting to login if (RadioButtonStudent.Checked == true) { List <StudentModel> studentLogInDetails = new List <StudentModel>(); try { //Attempt to retrieve the record from TblStudent in the database that exactly matches the data //input to TextBoxUsername and TextBoxPassword. SqlConnector db = new SqlConnector(); studentLogInDetails = db.GetStudent_ByLogInDetails(TextBoxUsername.Text, TextBoxPassword.Text); } catch { MyMessageBox.ShowMessage("Access to the database failed."); return; } if (studentLogInDetails.Count() == 1) { this.Hide(); StudentMainForm studentMainForm = new StudentMainForm(studentLogInDetails[0]); studentMainForm.ShowDialog(); this.Close(); } else { MyMessageBox.ShowMessage("Check your username and password."); } } else if (RadioButtonStaff.Checked == true) { List <StaffModel> staffLogInDetails = new List <StaffModel>(); try { SqlConnector db = new SqlConnector(); staffLogInDetails = db.GetStaff_ByLogInDetails(TextBoxUsername.Text, TextBoxPassword.Text); } catch { MyMessageBox.ShowMessage("Access to the database failed."); return; } if (staffLogInDetails.Count() == 1) { this.Hide(); StaffMainForm staffMainForm = new StaffMainForm(staffLogInDetails[0]); staffMainForm.ShowDialog(); this.Close(); } else { MyMessageBox.ShowMessage("Check your username and password."); } } } else { MyMessageBox.ShowMessage("Not all components validated successfully. Please check the flagged entries and try again."); } }
private void ButtonSendEmail_Click(object sender, EventArgs e) { ClassLibrary.CheckEmailIsValid(TextBoxEmail, ErrorProvider); //If the ErrorProvider has an error set, then display a message if (ErrorProvider.GetError(TextBoxEmail) != "") { MyMessageBox.ShowMessage("Not all components validated successfully. Please check the flagged entries and try again."); return; } else if (RadioButtonStudent.Checked == true) { List <StudentModel> students = new List <StudentModel>(); try { SqlConnector db = new SqlConnector(); students = db.GetStudent_All(); } catch { MyMessageBox.ShowMessage("Access to database failed."); return; } //Search each StudentModel in the student list. //If the email the user input matches an email in the //database, send the email. for (int i = 0; i < students.Count; i++) { if (students[i].StudentEmail == TextBoxEmail.Text) { ErrorProvider.SetError(TextBoxEmail, null); //Send email SendEmail(students[i].StudentEmail, students[i].StudentFirstName, students[i].StudentID, students[i].StudentPassword); MyMessageBox.ShowMessage("Email successfully sent."); return; } } } else if (RadioButtonStaff.Checked) { List <StaffModel> staffMembers = new List <StaffModel>(); try { SqlConnector db = new SqlConnector(); staffMembers = db.GetStaff_All(); } catch { MyMessageBox.ShowMessage("Access to database failed."); return; } for (int i = 0; i < staffMembers.Count; i++) { if (staffMembers[i].StaffEmail == TextBoxEmail.Text) { ErrorProvider.SetError(TextBoxEmail, null); //Send email SendEmail(staffMembers[i].StaffEmail, staffMembers[i].StaffFirstName, staffMembers[i].StaffEmail, staffMembers[i].StaffPassword); MyMessageBox.ShowMessage("Email successfully sent."); return; } } } MyMessageBox.ShowMessage("This email doesn't exist in the database."); }
/// <summary> /// Callback function called when data is received on the socket /// </summary> /// <param name="ar"></param> public void OnReceive(IAsyncResult ar) { string content = string.Empty; int bytesRead; // Retrieve the state object and the handler socket //from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket clientSocket = state.workSocket; if (clientSocket.Connected) { // Read data from the client socket. try { bytesRead = clientSocket.EndReceive(ar); if (bytesRead > 0) { if (!symmetricKeyReceived) { byte[] temp = state.buffer; int i = temp.Length - 1; while (temp[i] == 0) { --i; } // now data[i] is the last non-zero byte byte[] receivedData = new byte[i + 1]; Array.Copy(temp, receivedData, i + 1); //Get the symmetric key after decrypting it using RSA byte[] decryptedKey = rsa.Decrypt(receivedData, false); string decryptedKeyString = Convert.ToBase64String(decryptedKey); symmetricKey = decryptedKeyString; symmetricKeyReceived = true; SendStaffName(); clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); return; } // There might be more data, so store the data received so far. state.sb.Remove(0, state.sb.Length); state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); if (!studentNameReceived) { string encryptedStudentID = state.sb.ToString(); string sID = ClassLibrary.SymmetricEncryptDecrypt(encryptedStudentID, symmetricKey); try { SqlConnector db = new SqlConnector(); List <StudentModel> listStudents = db.GetStudent_ByStudentID(sID); connectedStudent = listStudents[0]; } catch { MyMessageBox.ShowMessage("Access to the database failed."); return; } SetTextBoxStudentName(); studentNameReceived = true; clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); return; } // Display text in rich text box string received = state.sb.ToString(); content = ClassLibrary.SymmetricEncryptDecrypt(received, symmetricKey); SetText(content); clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); } else { //Disconnect request has 0 bytes. //So if 0 byte message detected: disable further communication. SetSendButton(false); SetTextBoxConnectionStatus(Color.Red); clientStream.Dispose(); clientStream.Close(); connectedClient.Client.Dispose(); connectedClient.Client.Close(); } } catch (SocketException socketException) { //WSAECONNRESET, the other side closed impolitely if (socketException.ErrorCode == 10054 || ((socketException.ErrorCode != 10004) && (socketException.ErrorCode != 10053))) { // Complete the disconnect request. string remoteIP = ((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString(); string remotePort = ((IPEndPoint)clientSocket.RemoteEndPoint).Port.ToString(); this.ownerForm.DisconnectClient(remoteIP, remotePort); clientSocket.Close(); clientSocket = null; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } }
private void ButtonCreateMeeting_Click(object sender, EventArgs e) { //If all the controls validated correctly if (ValidateForm_CreateMeeting()) { int meetingLength; if (CheckBoxCustomLength.Checked) { meetingLength = int.Parse(TextBoxCustomLength.Text); } else { meetingLength = timeOptions[ListBoxMeetingLengths.SelectedIndex]; } //Get the staff member selected in the listbox StaffModel selectedStaffMember = staffMembers[ListBoxStaffList.SelectedIndex]; //Create a new meeting MeetingModel meeting = new MeetingModel( selectedStudent.StudentID, DateTimePickerDate.Value, DateTimePickerTime.Value, meetingLength, selectedStaffMember.StaffID); try { GlobalConfig.Connection.CreateMeeting(meeting); } catch { MyMessageBox.ShowMessage("Failed to add meeting to the database."); return; } MyMessageBox.ShowMessage("Successfully scheduled the meeting"); try { //Update the selected student's record in the database to show they have had a meeting before SqlConnector db = new SqlConnector(); db.UpdateIsNewStudent(selectedStudent.StudentID); } catch { MyMessageBox.ShowMessage("Failed to update student's status."); return; } TextBoxCustomLength.Clear(); //Email the participants SendEmail_ScheduleMeeting(meeting, selectedStudent.StudentEmail, selectedStaffMember.StaffFirstName, selectedStaffMember.StaffLastName); SendEmail_ScheduleMeeting(meeting, selectedStaffMember.StaffEmail, selectedStudent.StudentFirstName, selectedStudent.StudentLastName); } else { MyMessageBox.ShowMessage("Not all components validated successfully. Please check the flagged entries and try again."); } }
/// <summary> /// A callback function triggered when data is received on the socket. /// </summary> /// <param name="ar"></param> public void OnReceive(IAsyncResult ar) { string content = string.Empty; //Retrieve the state object and the handler socket from the asynchronous state object state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; int bytesRead; if (handler.Connected) { //Read data from the client socket try { bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //There might be more data, so store the data received so far state.sb.Remove(0, state.sb.Length); //Translate the bytes into a readable format state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); string s = state.sb.ToString(); //Before the server and client can begin chatting, the server must send its //public key over, the client then sends a generated symmetric key back, encrypted //using the public key. Then the server must send the staff member's name while //the client must send the student ID. if (!publicKeyReceived) { rsa = new RSACryptoServiceProvider(2048); //Save the public key received to rsa rsa.FromXmlString(state.sb.ToString()); SendSymmetricKey(); publicKeyReceived = true; //Continue to asynchronously receive data from the server handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); return; } if (!nameReceived) { SendStudentID(); string encryptedStaffName = state.sb.ToString(); staffName = ClassLibrary.SymmetricEncryptDecrypt(encryptedStaffName, symmetricKey); nameReceived = true; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); return; } //Display text in TextBox string received = state.sb.ToString(); content = ClassLibrary.SymmetricEncryptDecrypt(received, symmetricKey); //Function used to display text in the rich text box. A delegate function //must be used as we're not on the main thread. SetText(content); handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(OnReceive), state); } else { //If no data is received, then an error has occured as null messages cannot be sent. MyMessageBox.ShowMessage("Error occured: no data was supplied."); } } catch (SocketException socketEx) { //WSAECONNRESET: if the other side closes impolitely //(they shut down the server or crash for some reason) //Cut the connection and reset everything if (socketEx.ErrorCode == 10054 || ((socketEx.ErrorCode != 10004) && (socketEx.ErrorCode != 10053))) { handler.Close(); SetTextBoxConnectionStatusBackgroundColour(Color.Red); SetSendButton(false); SetConnectButton(true); SetDisconnectButton(false); serverStream.Close(); server.Close(); nameReceived = false; publicKeyReceived = false; } } catch (Exception ex) { //Anyother unexpected error is displayed here MyMessageBox.ShowMessage(ex.Message); } } else { handler.Close(); } }
/// <summary> /// Populate the listboxes. /// </summary> private void InitaliseList() { //First get the note IDs of the notes from the //database that contain the correct student ID try { SqlConnector db = new SqlConnector(); snl = db.GetStudentNoteLink_ByStudentID(student.StudentID); } catch { MyMessageBox.ShowMessage("Failed to load Student Note Link data from the database."); return; } //Display them in the appropriate listbox ListBoxNoteList.DataSource = snl; ListBoxNoteList.DisplayMember = "NoteNumber"; //If the snl list contains items, get the note //contents from the note IDs we just retrieved if (snl.Count() > 0) { try { SqlConnector db = new SqlConnector(); notes = db.GetNote_ByNoteID(snl[ListBoxNoteList.SelectedIndex].NoteID); } catch { MyMessageBox.ShowMessage("Failed to load notes from the database."); return; } //Display the selected note in TextBoxNoteViewer TextBoxNoteViewer.Text = notes[0].NoteContents; } else { TextBoxNoteViewer.Clear(); } //First get the message IDs of the messages from the //database that contain the correct student ID try { SqlConnector db = new SqlConnector(); sml = db.GetStudentMessageLink_ByStudentID(student.StudentID); } catch { MyMessageBox.ShowMessage("Failed to load Student Message Link data from the database."); return; } //Display these in the listbox ListBoxMessageList.DataSource = sml; ListBoxMessageList.DisplayMember = "MessageNumber"; //If sml list contains items, get the messages by //their message ID we just retrieved if (sml.Count() > 0) { try { SqlConnector db = new SqlConnector(); messages = db.GetMessage_ByMessageID(sml[ListBoxMessageList.SelectedIndex].MessageID); } catch { MyMessageBox.ShowMessage("Failed to load pinned messages from the database."); return; } //Display the selected message in the text box TextBoxMessageViewer.Text = messages[0].MessageContents; } else { TextBoxMessageViewer.Clear(); TextBoxDate.Clear(); TextBoxTime.Clear(); } }