// TODO: Exercise 1: Task 2b: Implement the Logon_Click event handler for the Logon button // Simulate logging on (no validation or authentication performed yet) public void Logon_Click(object sender, RoutedEventArgs e) { //bool isTeacherSigningIn = userrole.IsChecked.Value; //if (isTeacherSigningIn) //{ } 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 LogonSucces?.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; LogonSucces?.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 }
public void LogIn() { Logger.Debug($"Logger in to {ClientName} started"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(SessionUrl); request.Method = "POST"; request.MediaType = "application/json"; request.ContentType = "application/json"; AddAuthenticationData(request); try { using (IHttpResponse response = DataSource.GetResponse(request)) { var headers = response.Headers; if (headers.Keys.Any(k => k.Equals("Set-Cookie"))) { Match cookieMatch = CookieContents.Match(headers["Set-Cookie"]); if (cookieMatch.Success) { AuthCookie = new Cookie(CookieName, cookieMatch.Groups["contents"].Value, cookieMatch.Groups["path"].Value); Logger.Debug($"Logged in to {ClientName}"); LogonSucceeded?.Invoke(); } else { Logger.WarnFormat("Response to log in from {0} didn't contain a Set-Cookie in the format {1}.{2}Instead we got: {3}", ClientName, CookieContents, Environment.NewLine, headers["Set-Cookie"]); LogonFailed?.Invoke(); } } else { Logger.WarnFormat("Response to log in from {0} didn't contain a Set-Cookie header. Status received is {1}", ClientName, response.StatusCode); LogonFailed?.Invoke(); } } } catch (WebException we) { Logger.Error($"Error during log in to {ClientName}", we); LogonFailed?.Invoke(); } }
// 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); } }