public bool CheckPassword(int id, string password) { using (var db = new SchoolDbContext()) { var hashedPassword = CryptoHandler.SaltAndHashPassword(password); var admin = db.Administrators.FirstOrDefault(a => a.AdminId == id); if (admin != null) { return(admin.Password == hashedPassword); } else { Console.WriteLine($"User with id: {id} does not exist.."); return(false); } } }
public void NewAdmin(int id, string adminName, string password) { var hashedPassword = CryptoHandler.SaltAndHashPassword(password); using (var db = new SchoolDbContext()) { // creates a new admin var newAdmin = new Administrator { AdminId = id, // if id = 0 then the database will auto increment but if you select it manually it wont AdminName = adminName, Password = hashedPassword //uses the hashed password }; // adds the new admin to the table db.Administrators.Add(newAdmin); // saves changes to the db db.SaveChanges(); Console.WriteLine($"Admin {newAdmin.AdminName} has been registered."); } }