public async Task LoginCommand() { var connection = new UsernamePasswordConnection(_hostAddress, _userName, _view.LoginPasswordBox.Password); var manager = new YoutrackManager(connection); var error = await manager.CheckConnection().ConfigureAwait(false); if (error == null) { LogonSuccess?.Invoke(null, new LogonEventArgs(connection)); } }
// Validate the username and password against the Users collection in the MainWindow window private void Logon_Click(object sender, RoutedEventArgs e) { // Find the user in the list of possible users - first check whether the user is a Teacher // TODO: Exercise 1: Task 3a: Use the VerifyPassword method of the Teacher class to verify the teacher's password var teacher = (from Teacher t in DataSource.Teachers where string.CompareOrdinal(t.UserName, username.Text) == 0 && t.VerifyPassword(password.Password) == 0 select t).FirstOrDefault(); // If the UserName of the user retrieved by using LINQ is non-empty then the user is a teacher // TODO: Exercise 1: Task 3b: Check whether teacher is null before examining the UserName property if (teacher != null && !string.IsNullOrEmpty(teacher.UserName)) { // Save the UserID and Role (teacher or student) and UserName in the global context SessionContext.UserID = teacher.TeacherID; SessionContext.UserRole = Role.Teacher; SessionContext.UserName = teacher.UserName; SessionContext.CurrentTeacher = teacher; // Raise the LogonSuccess event and finish LogonSuccess?.Invoke(this, null); return; } // If the user is not a teacher, check whether the username and password match those of a student else { // TODO: Exercise 1: Task 3c: Use the VerifyPassword method of the Student class to verify the student's password var student = (from Student s in DataSource.Students where string.CompareOrdinal(s.UserName, username.Text) == 0 && s.VerifyPassword(password.Password) == 0 select s).FirstOrDefault(); // If the UserName of the user retrieved by using LINQ is non-empty then the user is a student // TODO: Exercise 1: Task 3d: Check whether student is null before examining the UserName property if (student != null && !string.IsNullOrEmpty(student.UserName)) { // Save the details of the student in the global context SessionContext.UserID = student.StudentID; SessionContext.UserRole = Role.Student; SessionContext.UserName = student.UserName; SessionContext.CurrentStudent = student; // Raise the LogonSuccess event and finish LogonSuccess?.Invoke(this, null); return; } } // If the credentials do not match those for a Teacher or for a Student then they must be invalid // Raise the LogonFailed event LogonFailed(this, null); }
// TODO: Exercise 3: Task 1a: Define LogonFailed event #endregion #region Logon Validation // TODO: Exercise 3: Task 1b: Validate the username and password against the Users collection in the MainWindow window private void Logon_Click(object sender, RoutedEventArgs e) { var teacherQuery = from Teacher teacher in DataSource.Teachers where teacher.UserName == username.Text && // User exists teacher.Password == password.Password // Password is correct select teacher; var studentQuery = from Student student in DataSource.Students where student.UserName == username.Text && student.Password == password.Password select student; if (teacherQuery.Count() == 1) // Only if there's exactly one match! { Teacher signedInTeacher = teacherQuery.First(); // Save current teacher in SessionContext SessionContext.UserName = signedInTeacher.UserName; SessionContext.UserID = signedInTeacher.TeacherID; SessionContext.UserRole = Role.Teacher; SessionContext.CurrentTeacher = signedInTeacher; // Raise succes event LogonSuccess?.Invoke(sender, e); // ? checker om LogonSucces er null (ingen lyttere) før den kalder invoke på alle. return; } else if (studentQuery.Count() == 1) { Student signedInStudent = studentQuery.First(); // Save current teacher in SessionContext SessionContext.UserName = signedInStudent.UserName; SessionContext.UserID = signedInStudent.StudentID; SessionContext.UserRole = Role.Student; SessionContext.CurrentStudent = signedInStudent; LogonSuccess?.Invoke(sender, e); // ? checker om LogonSucces er null (ingen lyttere) før den kalder invoke på alle. return; } LogonFailed?.Invoke(sender, e); // Raise failed event }
// TODO: Exercise 1: Task 2b: Implement the Logon_Click event handler for the Logon button // Simulate logging on (no validation or authentication performed yet) private void Logon_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(username.Text) && !string.IsNullOrEmpty(password.Password)) { Teacher teacher = DataSource.Teachers.FirstOrDefault(t => t.UserName == username.Text && t.Password == this.password.Password); if (teacher != null) { SessionContext.UserRole = Role.Teacher; SessionContext.UserID = teacher.TeacherID; SessionContext.UserName = teacher.UserName; SessionContext.CurrentTeacher = teacher; LogonSuccess?.Invoke(this, null); } else { Student student = DataSource.Students.FirstOrDefault(s => s.UserName == username.Text && s.Password == this.password.Password); if (student != null) { SessionContext.UserRole = Role.Student; SessionContext.UserID = student.StudentID; SessionContext.UserName = student.UserName; SessionContext.CurrentStudent = student; LogonSuccess?.Invoke(this, null); } else { LogonFailed?.Invoke(this, null); } } } else { LogonFailed?.Invoke(this, null); } }
// TODO: Exercise 3: Task 1b: Validate the username and password against the Users collection in the MainWindow window private void Logon_Click(object sender, RoutedEventArgs e) { var user = username.Text; var pass = password.Password; var teacher = (DataSource.Teachers.Cast <Teacher>() .Where(t => String.CompareOrdinal(t.UserName, user) == 0 && String.CompareOrdinal(t.Password, pass) == 0)) .FirstOrDefault(); if (teacher.UserName != null) { SessionContext.UserRole = Role.Teacher; SessionContext.UserID = teacher.TeacherID; SessionContext.UserName = teacher.UserName; SessionContext.CurrentTeacher = teacher; LogonSuccess?.Invoke(this, null); } else { LogonFailed?.Invoke(this, null); } }