private void Listen_Load(object sender, EventArgs e) { while (true) { LogInSingUp login = new LogInSingUp(ip, port); //load log in form login.ShowDialog(); //closes the main form if (login.DialogResult == DialogResult.OK) { username = login.Username; password = login.Password; ip = login.IP; UserAuthenticationResult result = UserAuthenticationResult.UnknownResult; try { // Open connection to server and attempt to validate the user. serverConnection = new ServerConnection(ip, port); serverConnection.AudioListReceived += new ServerConnection.TrackListEventHandler(serverConnection_AudioListReceived); serverConnection.AudioPacketReceived += new ServerConnection.DataReceivedEventHandler(serverConnection_AudioPacketReceived); //valadate log in information serverConnection.OpenConnection(); result = serverConnection.Authenticate(username, password); if (result == UserAuthenticationResult.Success) { serverConnection.RequestAudioList(); return; } } catch (Exception) { MessageBox.Show("There was a problem connecting to the server."); } if (result != UserAuthenticationResult.Success) { MessageBox.Show("Connection or validation error."); } } else { this.Close(); break; } } }
// verify log in information when button clicked private void logInButton_Click(object sender, EventArgs e) { username = userNameBox.Text; password = passwordBox.Text; if (password == "" || username == "")// make sure textboxes are not empty { MessageBox.Show("Please enter information in the User Name and Password fields"); return; } else//when information is correct { //connect to server UserAuthenticationResult result = UserAuthenticationResult.UnknownResult; try { // Open connection to server and attempt to validate the user. using (ServerConnection serverConnection = new ServerConnection(ip, port)) { //valadate log in information serverConnection.OpenConnection(); result = serverConnection.Authenticate(username, password); } } catch (Exception) { MessageBox.Show("There was a problem connecting to the server."); return; } switch (result) { case UserAuthenticationResult.UnknownResult: MessageBox.Show("I'm not sure what happened."); break; case UserAuthenticationResult.Success://if log in info is valadated go to program this.DialogResult = DialogResult.OK; this.Close(); break; case UserAuthenticationResult.InvalidUser://if username info not valadated MessageBox.Show("User Name not Accepted"); userNameBox.Clear(); passwordBox.Clear(); break; case UserAuthenticationResult.InvalidPassword://if password info not valadated MessageBox.Show("Password not Accepted"); userNameBox.Clear(); passwordBox.Clear(); break; default: break; } } }
private bool TryCheckConnection() { if (serverConnection.Connected) { return true; } try { serverConnection = new ServerConnection(ip, port); serverConnection.AudioListReceived += new ServerConnection.TrackListEventHandler(serverConnection_AudioListReceived); serverConnection.AudioPacketReceived += new ServerConnection.DataReceivedEventHandler(serverConnection_AudioPacketReceived); serverConnection.OpenConnection(); if (serverConnection.Authenticate(username, password) == UserAuthenticationResult.Success) { return true; } } catch (Exception ex) { MessageBox.Show("There was a problem with the connection to the audio server: \n" + ex.Message); } return false; }
private void registerButton_Click(object sender, EventArgs e) { password = NewPasswordBox.Text; username = newUserNameBox.Text; int Num = 0; int Lett = 0; for (int i = 0; i < password.Length; i++) { if (char.IsNumber(password[i])) { Num++; } else if (char.IsLetter(password[i])) { Lett++; } } if (username == "" || password == "" || newCPWBox.Text == "")//checks for text in feilds { MessageBox.Show("Please enter information in the User Name, Password and Confirm Password Fields"); } else if (password.Length < 6)//checks if pass word is longer then seven { MessageBox.Show("Password needs to be longer then 6 characters"); NewPasswordBox.Clear(); newCPWBox.Clear(); } else if (Num < 2) { MessageBox.Show("Need at least 2 numbers in password"); NewPasswordBox.Clear(); newCPWBox.Clear(); } else if (Lett < 3) { MessageBox.Show("Need at least 3 letters in password"); NewPasswordBox.Clear(); newCPWBox.Clear(); } else if (password != newCPWBox.Text)//makes sure password is enter correct { MessageBox.Show("The Password Field and Confirm Password Field do not Match.\nPlease Try Again"); NewPasswordBox.Clear(); newCPWBox.Clear(); } else { UserAddResult result = UserAddResult.UnknownResult; try { using (ServerConnection serverConnection = new ServerConnection(ip, port)) { serverConnection.OpenConnection(); result = serverConnection.Register(username, password); } } catch (Exception) { MessageBox.Show("There was a problem connecting to the server."); } switch (result)// get a resuslt from server that username and password can be created { case UserAddResult.UnknownResult: break; case UserAddResult.Success://if vailadated MessageBox.Show(" Congraulations! You Are Now Registered."); this.DialogResult = DialogResult.OK; break; case UserAddResult.AlreadyExists://if user name is alredy in use MessageBox.Show("Username not Avaiable. "); newCPWBox.Clear(); newUserNameBox.Clear(); NewPasswordBox.Clear(); break; case UserAddResult.InvalidPassword://if password is invalid password MessageBox.Show("Invalied Password. "); newCPWBox.Clear(); newUserNameBox.Clear(); NewPasswordBox.Clear(); break; case UserAddResult.UsernameTooLong:// username to long MessageBox.Show("User Name Too Long. "); newCPWBox.Clear(); newUserNameBox.Clear(); NewPasswordBox.Clear(); break; default: break; } } }