public static User authenticate(Credentials credentials) { User user = null; DAL dalDataContext = new DAL(); try { user = (from users in dalDataContext.users where users.UserID == credentials.UserID select users).FirstOrDefault<User>(); if (user == null) { throw new FaultException<SException>(new SException(), new FaultReason("Invalid User, Please try again")); } else if (user.User_Password.CompareTo(KeyGen.Decrypt(credentials.Password)) != 0) { throw new FaultException<SException>(new SException(), new FaultReason("Wrong Password!, please try again")); } else { user.GetSystemRole(); } } catch (InvalidOperationException ex) { throw new FaultException<SException>(new SException(), new FaultReason("An error had occured: " + ex.Message)); } return user; }
private void btnLogin_Click(object sender, RoutedEventArgs e) { if (txtUserID.Text.Trim().Length == 0) { MessageBox.Show("Please enter your user id", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } if (txtPassword.Password.Trim().Length == 0) { MessageBox.Show("Please enter your password!", "Invalid Input", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } if (String.Compare(txtSvAddr.Text.Trim(), ConfigHelper.GetEndpointAddress(), true) != 0) { ConfigHelper.SaveEndpointAddress(txtSvAddr.Text.Trim()); } AdminHelper client = new AdminHelper(); try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); KeyValueConfigurationCollection settings = config.AppSettings.Settings; settings["timeout"].Value = client.GetClientTimeOut().ToString(); config.Save(ConfigurationSaveMode.Modified); //relaod the section you modified ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); //User u = client.Authenticate(txtUserID.Text.Trim(), txtPassword.Password); Credentials c = new Credentials(); c.UserID = txtUserID.Text.Trim(); c.Password = Helper.KeyGen.Encrypt(txtPassword.Password); User u = client.SecureAuthenticate(c); var admForm = new frmMain(u, this); this.Visibility = Visibility.Collapsed; this.txtPassword.Clear(); this.txtUserID.Clear(); this.txtUserID.Focus(); admForm.Show(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { client.Close(); // Always close the client. } }
bool AuthNUSNET(string username, string password) { AdministrationClient client = new AdministrationClient(); Credentials c = new Credentials(); c.UserID = Login1.UserName.Trim(); c.Password = KeyGen.Encrypt(Login1.Password); try { User u = client.SecureAuthenticate(c); client.Close(); Session["nusNETuser"] = u; client.Close(); return true; } catch (Exception ex) { Alert.Show(ex.Message, false); } finally { client.Close(); } return false; }
public User SecureAuthenticate(Credentials credentials) { User user = null; try { user = UserController.authenticate(credentials); if (user == null) { throw new FaultException<SException>(new SException(), new FaultReason("Invalid login details, please try again")); } return user; } catch (Exception e) { Console.WriteLine(e.StackTrace); throw new FaultException<SException>(new SException(), new FaultReason(e.Message)); } }