//this vlaidates the user whether they have encrypted, hashed or normal records displayed in the database and returns the username as a string variable public string validateLogin(string username, string password, string passwordAlt) { string validateUser = "******"; //Account validate = DBcontext.Account.FirstOrDefault(a => a.Username == username); string[] passwords = new string[Accounts.Count()]; string[] usernames = new string[Accounts.Count()]; List <Account> accountDetails = new List <Account>(); accountDetails = Accounts.ToList(); int count = Accounts.Count(); for (int j = 0; j < count; j++) { usernames[j] = accountDetails[j].Username.ToString(); passwords[j] = accountDetails[j].Password.ToString(); } int i = 0; do { try { //this creates the string variable comparer StringComparer comparer = StringComparer.OrdinalIgnoreCase; //this checks var user = Encrypted.decrypt(usernames[i].ToString()).ToString();//protectionService.UnProtect(usernames[i]);/*protectionService.UnProtect(usernames[i])*/ if (user == username) { if (0 == comparer.Compare(password, passwords[i])) { validateUser = usernames[i];//true; return(validateUser); } else { validateUser = "******";//validateUser = false; //i++; } } } catch { if (usernames[i] == username && (passwords[i] == password || passwords[i] == passwordAlt)) { validateUser = usernames[i];//true; return(validateUser); } else { validateUser = "******";// false; //i++; } } i++; } while (i < count); validateUser = "******"; Account validate = DBcontext.Account.FirstOrDefault(a => a.Username == username); //Account validate = DBcontext.Account.FirstOrDefault(a => Encrypted.decrypt(a.Username) == username);//_protector.Unprotect() //Account validate = DBcontext.Account.FirstOrDefault(a => a.Username == _protector.Protect(username)); /*if (validate.Username == username)//&& validate.Password == password)/*_protector.Unprotect()*//*_protector.Unprotect(*//*)*///(validate.Username == _protector.Protect(username) && validate.Password == _protector.Protect(password)) //if(validate.Password.Equals(login.Password)) /*{ * StringComparer comparer = StringComparer.OrdinalIgnoreCase; * if(0 == comparer.Compare(password, validate.Password)) * { * validateUser = true; * } * else * { * validateUser = false; * } * } * else * { * validateUser = false; * }*/ return(validateUser); }
public IActionResult LogIn(Login login) { //this checks if username or password has been left empty if (login.Password == null || login.Username == null) { TempData["Error"] = "Require Valid Login Details"; return(View()); } string password; //this hashes the password variable temporarily stored in the login model if there is a password using (MD5 hash = MD5.Create()) { password = GetMd5Hash(hash, login.Password); } //string username = /*/*Encrypted.encrypt protect.Protect*/ protect.Protect(login.Username); //this assigns the string returned from validating the user to a new string called username string username = accountRepository.validateLogin(login.Username, password, login.Password); if (username != " ") // == true) //(login.Username, password) == true)//,login.Password) == true) /*_protector,*///_protector.Protect(login.Username), _protector.Protect(login.Password)) == true) { Account account = new Account(); Address address = new Address(); account = accountRepository.Accounts.FirstOrDefault(u => u.Username == username); //Encrypted.decrypt(u.Username) == login.Username); address = addressRepository.address.FirstOrDefault(a => a.AddressID == account.AddressID); ContactDetails contactDetails = new ContactDetails(); contactDetails = CDRepository.ContactDetails.FirstOrDefault(c => c.ContactDetailsID == account.ContactID); try { //this decrypts the string variables that had been encrypted so that the fields can be autofilled when creating an appointment Account accounts = new Account(); accounts.RoleID = account.RoleID; accounts.MedicalPersonnel = Encrypted.decrypt(account.MedicalPersonnel); // protect.Protect("New Doctor");/*Encrypted.encrypt protect.Protect*/ //_protector.Protect("New Doctor"); accounts.Name = Encrypted.decrypt(account.Name.ToString()); /*Encrypted.encryptprotect.Protect*/ /*protect.Protect*/ //_protector.Protect(account.Name); //_protector.Protect(account.Name); accounts.Username = login.Username; /*Encrypted.encrypt protect.Protect*/ //protect.Protect(account.Username.ToString());//;_protector.Protect(account.Username); if (account.CHINumber != null) { accounts.CHINumber = Encrypted.decrypt(account.CHINumber.ToString()); /*Encrypted.encrypt protect.Protect*/ //protect.Protect(account.CHINumber.ToString());//_protector.Protect(account.CHINumber); } else { accounts.CHINumber = null; } accounts.DOB = account.DOB; // _protector.Protect(account.DOB.ToString()); Address addresses = new Address(); addresses.StreetName = Encrypted.decrypt(address.StreetName.ToString()); /*/*Encrypted.encrypt protect.Protect*/ //protect.Protect(address.StreetName.ToString());// _protector.Protect(address.StreetName); addresses.Region = Encrypted.decrypt(address.Region.ToString()); /*/*Encrypted.encrypt protect.Protect*/ //protect.Protect(address.Region.ToString());//_protector.Protect(address.Region); addresses.Postcode = Encrypted.decrypt(address.Postcode.ToString()); /*/*Encrypted.encrypt protect.Protect*/ //protect.Protect(address.Postcode.ToString());//_protector.Protect(address.Postcode); if (address.County == " ") { addresses.County = " "; } else { addresses.County = Encrypted.decrypt(address.County.ToString()); /*/*Encrypted.encrypt protect.Protect*/ //protect.Protect(address.County.ToString());// _protector.Protect(address.County); } ContactDetails cds = new ContactDetails(); if (contactDetails.HomePhone == null) { cds.HomePhone = null; } else { cds.HomePhone = Encrypted.decrypt(contactDetails.HomePhone.ToString()); /*/*Encrypted.encrypt protect.Protect*/ // protect.Protect(contactDetails.HomePhone.ToString());// _protector.Protect(contactDetails.HomePhone); } if (contactDetails.WorkPhone != null) { cds.WorkPhone = Encrypted.decrypt(contactDetails.WorkPhone.ToString()); /*/*Encrypted.encrypt protect.Protect*/ //protect.Protect(contactDetails.WorkPhone.ToString());// _protector.Protect(contactDetails.WorkPhone); } else { cds.WorkPhone = null; } if (contactDetails.MobilePhone == null) { cds.MobilePhone = null; } else { cds.MobilePhone = Encrypted.decrypt(contactDetails.MobilePhone.ToString()); /*/*Encrypted.encrypt protect.Protect*/ // protect.Protect(contactDetails.MobilePhone.ToString());// _protector.Protect(contactDetails.MobilePhone); } cds.NextOfKin = Encrypted.decrypt(contactDetails.NextOfKin.ToString()); /*if (contactDetails.Email == null) * { * cds.Email = null;// contactDetails.Email.ToString();// null; * } * else * { * //this is an example of trying to decrypt an email string but was unsuccessful as it caused an error * string email = Encrypted.decrypt(contactDetails.Email); * cds.Email = email;//Encrypted.decrypt(contactDetails.Email.ToString());/*/ //Encrypted.encrypt protect.Protect*/ //protect.Protect(contactDetails.Email.ToString());// _protector.Protect(contactDetails.Email); //} //if the string decryption could not be completed then the models would be passed to the session cds.Email = contactDetails.Email.ToString(); HttpContext.Session.setJson("Account", accounts); HttpContext.Session.setJson("Address", addresses); HttpContext.Session.setJson("CD", cds); HttpContext.Session.SetString("Name", accounts.Name); string role = roleRepository.getRole(accounts.RoleID); HttpContext.Session.SetString("Type", role); } catch { //if the string decryption could not be completed then the models would be passed to the session HttpContext.Session.setJson("Account", account); HttpContext.Session.setJson("Address", address); HttpContext.Session.setJson("CD", contactDetails); HttpContext.Session.SetString("Name", account.Name); string role = roleRepository.getRole(account.RoleID); HttpContext.Session.SetString("Type", role); } //this checks if the user is a patient or not and redirects them to the appropriate home page if (account.RoleID == 3 || account.RoleID == 4 || account.RoleID == 6) { return(RedirectToAction("Index", "Practitioners")); } else { return(RedirectToAction("Index", "Patient")); } //return RedirectToPage(); ////address = addressRepository.address.Where(a => a.AddressID == id[2]); //return RedirectToAction("~/Views/Patient/Index?ids=" + id); /*return RedirectToAction("../Patient/Index");*//*, /*accountRepository.Accounts.Where(l => l.ID == id[0]), /*); 23:11 13/01/2019, addressRepository.address.Where(l => l.AddressID == id[1]), CDRepository.ContactDetails.Where(cd => cd.ContactDetailsID == id[2])));/*account.ContactID * /*,accountRepository.Accounts.Where(l => l.ID == id[0])*///);// 23:11 13/01/2019, addressRepository.address.Where(l => l.AddressID == id[1]), CDRepository.ContactDetails.Where(cd => cd.ContactDetailsID == id[2])));/*account.ContactID*/ } else { //this creates a temporary message that tells the user that their login details are incorrect and returns the view to the user TempData["Error"] = "Login Details Incorrect"; return(View()); } }