public MyModel(WCFClient wCFClient) { this._wcfClient = wCFClient; this.AddBookCommand = new DelegatedCommand(this.AddBookCommandImpl); this.FindBookByIsbnCommand = new DelegatedCommand(this.FindBookByIsbnCommandImpl); this.FindBookByKeywordsCommand = new DelegatedCommand(this.FindBookByKeywordsCommandImpl); this.SaveCommand = new DelegatedCommand(this.SaveCommandImpl); this.DownloadAll(); }
static void Main(string[] args) { Console.ReadKey(); NetTcpBinding binding2 = new NetTcpBinding(); ICMSCommunication proxy2 = new ChannelFactory <ICMSCommunication>(binding2, new EndpointAddress("net.tcp://localhost:1324/CMS")).CreateChannel(); string tempCertificate = WindowsIdentity.GetCurrent().Name; string[] parse = tempCertificate.Split('\\'); string clientCertificateName = parse[1]; bool temp = false; int num = proxy2.NumOfCertificates(clientCertificateName); X509Certificate2 clientCertificate = ClientGet.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, clientCertificateName); if (clientCertificate != null) { if (!proxy2.CheckValidation(clientCertificate)) { clientCertificate = null; } } if (clientCertificate == null) { clientCertificate = ClientGet.GetCertificateFromStorage(StoreName.My, StoreLocation.LocalMachine, clientCertificateName + "New" + num); if (clientCertificate != null) { if (!proxy2.CheckValidation(clientCertificate)) { clientCertificate = null; } } } if (clientCertificate == null) { Console.WriteLine("Enter new certificate password: "******"Sertifikat postoji i validan je."); } else { Console.WriteLine("Sertifikat postoji i NIJE validan."); } } NetTcpBinding binding = new NetTcpBinding(); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; X509Certificate2 srvCert = ClientGet.GetCertificateFromTrustedPeople(StoreName.TrustedPeople, StoreLocation.LocalMachine); EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:1234/Server"), new X509CertificateEndpointIdentity(srvCert)); Thread.Sleep(3000); using (WCFClient proxy = new WCFClient(binding, address, clientCertificateName, proxy2)) { Random r = new Random(); Console.WriteLine("\n********* Menu **********"); Console.WriteLine("* 1. Withdrawal certificate. *"); Console.WriteLine("* 2. Establish communication. *"); Console.WriteLine("* 3. Exit *"); Console.WriteLine("**********************************"); Console.WriteLine("Select option: "); int select = int.Parse(Console.ReadLine()); if (select == 1) { Console.WriteLine("Enter new certificate password: "******"ERROR: Selected option error!"); } } Console.WriteLine("INFO: Enter key to exit."); Console.ReadKey(); }
static void Main(string[] args) { /// Define the expected service certificate. It is required to establish cmmunication using certificates. string srvCertCN = "sbesserver"; NetTcpBinding binding = new NetTcpBinding(); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; /// Use CertManager class to obtain the certificate based on the "srvCertCN" representing the expected service identity. X509Certificate2 srvCert = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, srvCertCN); EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:9999/Receiver"), new X509CertificateEndpointIdentity(srvCert)); int id; string name = ""; string dateInput = ""; DateTime date = DateTime.Now; int room = -1; double price = -1; int discount = -1; int ticketQuantity = -1; string[] tokens; int input = 0; using (WCFClient proxy = new WCFClient(binding, address)) { Console.WriteLine("Connection established."); do { Console.WriteLine("\nMenu:\n\t1. Add Performance\n\t2. Modify Performance\n\t3. Modify Discount\n\t" + "4. Make Reservation\n\t5. Pay Reservation\n\t6. Show all performances\n\t" + "7. Show all users\n\t8. Show all reservations\n\t9. Show discount\n\t10. Show my informations\n\t" + "11. Exit\n\t\n======================\n"); try { input = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Invalid input: {e.Message}."); input = 0; } switch (input) { case 1: Console.Write("\nNew Performance\n\tname: "); try { name = Console.ReadLine(); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } Console.Write("\tdate (format dd/mm/yyyy): "); try { dateInput = Console.ReadLine(); tokens = dateInput.Split('/'); date = new DateTime(int.Parse(tokens[2]), int.Parse(tokens[1]), int.Parse(tokens[0])); } catch (Exception) { Console.WriteLine($"Input not valid. Please try again and enter date in format DD/MM/YYYY."); break; } Console.Write("\troom: "); try { room = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } Console.Write("\tticket price: "); try { price = double.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } proxy.AddPerformance(name, date, room, price, out id); break; case 2: Console.WriteLine("\nEnter id of the performance you want to modify: "); try { id = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } if (!proxy.CheckIfPerformanceExists(id, 2)) { break; } Console.Write("\tname: "); try { name = Console.ReadLine(); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } Console.Write("\tdate (format dd/mm/yyyy): "); try { dateInput = Console.ReadLine(); tokens = dateInput.Split('/'); date = new DateTime(int.Parse(tokens[2]), int.Parse(tokens[1]), int.Parse(tokens[0])); } catch (Exception e) { Console.WriteLine($"Input not valid. Please try again and enter date in format DD/MM/YYYY."); break; } Console.Write("\troom: "); try { room = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } Console.Write("\tticket price: "); try { price = double.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } proxy.ModifyPerformance(id, name, date, room, price); break; case 3: Console.Write("\nEnter new discount: "); try { discount = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } proxy.ModifyDiscount(discount); break; case 4: Console.WriteLine("\nEnter id of the performance you want to reserve: "); try { id = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } if (!proxy.CheckIfPerformanceExists(id, 4)) { break; } Console.Write("\n\tticketQuantity: "); try { ticketQuantity = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } proxy.MakeReservation(id, DateTime.Now, ticketQuantity, out int reservationId); break; case 5: Console.WriteLine("\nEnter id of the reservation you want to pay: "); try { id = int.Parse(Console.ReadLine()); } catch (Exception e) { Console.WriteLine($"Input not valid {e.Message}. Please try again."); break; } if (!proxy.CheckIfReservationCanBePaied(id)) { break; } proxy.PayReservation(id); break; case 6: proxy.ListAllPerformances(); break; case 7: proxy.ListAllUsers(); break; case 8: proxy.ListAllReservations(); break; case 9: proxy.ListDiscount(); break; case 10: proxy.ListUser(); break; case 11: break; default: Console.WriteLine("Entered invalid number. Valid 1-11. Please try again."); break; } }while (input != 11); proxy.Dispose(); proxy.Close(); } }
// TODO: public static void Menu(WCFClient proxy) { while (true) { Console.Clear(); Console.WriteLine("1. GetBill."); Console.WriteLine("2. ModifyID."); Console.WriteLine("3. ModifyReading."); Console.WriteLine("4. DeleteEntity."); Console.WriteLine("5. DeleteDB."); Console.WriteLine("6. AddDB."); Console.WriteLine("-----------------------"); Console.Write("Enter your choice: "); int choice = 0; if (!int.TryParse(Console.ReadLine(), out choice)) { continue; } int id = 0, newID = 0; double newReading = 0; switch (choice) { case 1: Console.Write("Enter ID: "); if (int.TryParse(Console.ReadLine(), out id)) { proxy.GetBill(id); } break; case 2: Console.Write("Enter ID: "); if (int.TryParse(Console.ReadLine(), out id)) { Console.Write("Enter new id: "); if (int.TryParse(Console.ReadLine(), out newID)) { proxy.ModifyID(newID, id); } } break; case 3: Console.Write("Enter ID: "); if (int.TryParse(Console.ReadLine(), out id)) { Console.Write("Enter new reading: "); if (double.TryParse(Console.ReadLine(), out newReading)) { proxy.ModifyReading(newReading, id); } } break; case 4: Console.Write("Enter ID: "); if (int.TryParse(Console.ReadLine(), out id)) { proxy.DeleteEntityDB(id); } break; case 5: proxy.DeleteDB(); break; case 6: Console.Write("Enter ID: "); if (int.TryParse(Console.ReadLine(), out id)) { Console.Write("Enter full name: "); var name = Console.ReadLine(); Console.Write("Enter reading: "); if (double.TryParse(Console.ReadLine(), out newReading)) { proxy.AddDB(id, name, newReading); } } break; default: break; } Console.Write("Press <enter> continue..."); Console.ReadLine(); } }
static void Main(string[] args) { string srvCertCN = "sbesservice"; NetTcpBinding binding = new NetTcpBinding(); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate; X509Certificate2 srvCert = Manager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, srvCertCN); EndpointAddress address = new EndpointAddress(new Uri("net.tcp://localhost:9999/Receiver"), new X509CertificateEndpointIdentity(srvCert)); Dictionary <int, string> idMap = new Dictionary <int, string>(); Console.WriteLine("Korisnik {0} je pokrenuo klijenta", WindowsIdentity.GetCurrent().Name); using (WCFClient proxy = new WCFClient(binding, address)) { while (true) { string option = ShowMenu(); if (option == "1") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (proxy.CreateDataBase(path)) { Console.WriteLine("Database created successfuly"); } } else if (option == "2") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } string id = Guid.NewGuid().ToString(); Console.Write("Enter region: "); string region = Console.ReadLine(); Console.Write("Enter city: "); string city = Console.ReadLine(); Console.Write("Enter year: "); int year = int.Parse(Console.ReadLine()); Console.Write("Enter usage: "); double usage = double.Parse(Console.ReadLine()); EnergyConsumptionModel e1 = new EnergyConsumptionModel() { identificator = id, region = region, city = city, year = year, usageOfElectricEnergyPerYear = usage }; if (proxy.Add(path, e1)) { Console.WriteLine("Item added successfully"); } } else if (option == "3") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } List <EnergyConsumptionModel> items = proxy.Read(path); if (items == null) { Console.WriteLine("Database is empty!"); continue; } int itemCounter = 1; for (int i = 0; i < items.Count; i++) { Console.WriteLine("---------------------"); Console.WriteLine($"ID: {itemCounter++}"); Console.WriteLine(items[i].region); Console.WriteLine(items[i].city); Console.WriteLine(items[i].year); Console.WriteLine(items[i].usageOfElectricEnergyPerYear); } } else if (option == "4") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); Console.WriteLine(); continue; } Console.Write("Enter item ID: "); int id = int.Parse(Console.ReadLine()); idMap = CreateIdMap(proxy, path); if (proxy.Delete(path, idMap[id])) { Console.WriteLine($"Item with id: {id} deleted succesfully"); } } else if (option == "5") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } Console.Write("Enter item ID: "); int id = int.Parse(Console.ReadLine()); idMap = CreateIdMap(proxy, path); var energyModel = proxy.ReadItem(path, idMap[id]); if (energyModel == null) { continue; } Console.Write("Enter region: "); string region = Console.ReadLine(); Console.Write("Enter city: "); string city = Console.ReadLine(); Console.Write("Enter year: "); int year = int.Parse(Console.ReadLine()); Console.Write("Enter usage: "); double usage = double.Parse(Console.ReadLine()); energyModel.region = region; energyModel.city = city; energyModel.year = year; energyModel.usageOfElectricEnergyPerYear = usage; if (proxy.Update(path, energyModel)) { Console.WriteLine($"Item with id: {id} updated succesfully"); } } else if (option == "6") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } Console.WriteLine("Enter city:"); string city = Console.ReadLine(); double result = proxy.AverageConsumptionPerCity(path, city); if (result == -1) { Console.WriteLine("There is no energy consumption records for {0}", city); } else { Console.WriteLine("Average: {0}", result); } } else if (option == "7") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } Console.WriteLine("Enter region:"); string region = Console.ReadLine(); double result = proxy.AverageConsumptionPerRegion(path, region); if (result == -1) { Console.WriteLine("There is no energy consumption records for {0}", region); } else { Console.WriteLine("Average: {0}", result); } } else if (option == "8") { Console.WriteLine("Enter path:"); string path = Console.ReadLine(); if (!proxy.DatabaseExists(path)) { Console.WriteLine("Database does not exists!"); continue; } Console.WriteLine("Enter region:"); string region = Console.ReadLine(); string result = proxy.MaxConsumptionPerRegion(path, region); if (result == null) { Console.WriteLine("There is no energy consumption records for {0}", region); } else { Console.WriteLine("Result: {0}", result); } } } } }
private static void SelectOption(WCFClient proxy, string option) { //Debugger.Launch(); string databaseName = String.Empty; string returnedValueString = String.Empty; string city = String.Empty; string country = String.Empty; string payday = String.Empty; string temp = String.Empty; string message = String.Empty; byte[] signature; short fromAge; short toAge; if (option != "9") { Console.Write("\nEnter database name: "); databaseName = Console.ReadLine(); } switch (option) { case "1": returnedValueString = proxy.CreateDatabase(databaseName); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "2": returnedValueString = proxy.DeleteDatabase(databaseName); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "3": message = CreateMessage(databaseName, "Insert"); signature = DigitalSignature.Create(message, proxy.Credentials.ClientCertificate.Certificate); returnedValueString = proxy.Insert(message, signature); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "4": message = CreateMessage(databaseName, "Edit"); signature = DigitalSignature.Create(message, proxy.Credentials.ClientCertificate.Certificate); returnedValueString = proxy.Edit(message, signature); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "5": returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewAll(databaseName)); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "6": returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewMaxPayed(databaseName)); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "7": Console.Write("Country: "); country = Console.ReadLine(); do { Console.Write("Payday: "); payday = Console.ReadLine(); } while (!Int32.TryParse(payday, out int id)); returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.AverageSalaryByCountryAndPayday(databaseName, country, payday)); //returnedValueString = proxy.AverageSalaryByCountryAndPayday(databaseName, country, payday); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "8": Console.Write("City: "); city = Console.ReadLine(); do { do { Console.Write("From age: "); temp = Console.ReadLine(); } while (!short.TryParse(temp, out fromAge)); do { Console.Write("To age: "); temp = Console.ReadLine(); } while (!short.TryParse(temp, out toAge)); } while (fromAge > toAge); returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.AverageSalaryByCityAndAge(databaseName, city, fromAge, toAge)); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "9": returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewDatabasesNames()); Console.WriteLine(Environment.NewLine + returnedValueString); break; case "10": Console.WriteLine("Exit"); break; default: Console.WriteLine("Unknown command"); break; } }
static int Main(string[] args) { string users = String.Empty; string clientId; int otherClient; string encryptedSecretKey = String.Empty; string decryptedSecretKey; RSA_Asimm_Algorithm_C rsa = new RSA_Asimm_Algorithm_C(); string publicKey = rsa.GenerateKeys(); WCFClient proxy = BindToCentralServer(); Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); IIdentity identity = Thread.CurrentPrincipal.Identity; WindowsIdentity winIdentity = identity as WindowsIdentity; string senderName = Formatter.ParseName(identity.Name); int peerServicePort = 6000; try { clientId = proxy.Connect(identity.Name, winIdentity.User.ToString()); } catch (Exception e) { Console.WriteLine("Connection failed. \nDetails: " + e.Message); Console.WriteLine("Press any key to close connection. "); Console.ReadKey(); proxy.Abort(); return(0); } peerServicePort += Int32.Parse(clientId); Console.WriteLine("Current: peer_" + clientId); ServiceHost host = OpenPeerService(peerServicePort); MonitoringChannel proxyMonitoring = OpenMonitoringChannel(); //open channel for logging all messages try { encryptedSecretKey = proxy.GenerateSecretKey(publicKey); //klijent dobija kriptovan tajni kljuc } catch (Exception e) { Console.WriteLine("Encryption of secret key failed. \nDetails: " + e.Message); Console.WriteLine("Press any key to close connection. "); Console.ReadKey(); proxy.Abort(); return(0); } decryptedSecretKey = rsa.DecryptData(encryptedSecretKey); string publicKeyFromMonitoring = String.Empty; try { publicKeyFromMonitoring = proxyMonitoring.GenerateRSAKeys(); } catch (Exception e) { Console.WriteLine("Getting public RSA key from monitoring failed. \nDetails: " + e.Message); } string encryptedSecretKeyWithMonitoringKey = rsa.EncryptData(publicKeyFromMonitoring, decryptedSecretKey); try { proxyMonitoring.SendSecretKey(encryptedSecretKeyWithMonitoringKey); } catch (FaultException e) { Console.WriteLine("Sending secret key to monitoring failed. \nDetails: " + e.Message); } while (true) { int m = Menu(); if (m == 1) { try { users = proxy.GetConnectedClients(); } catch (FaultException e) { Console.WriteLine("Getting list of connected clients from central server failed. \nDetails: " + e.Message); } DeserializeJson(users); PrintConnectedClients(); otherClient = ChooseClient();//otherUser i otherClient se ne podudaraju ali otherUser = dict vrednost sa keyom otherClient if (otherClient == 0) { continue; } User otherUser = DBClients.connectedClients[otherClient]; if (otherUser.Counter == Int32.Parse(clientId)) { Console.WriteLine("This is your account. Please choose again."); continue; } Peer proxyPeerClient = OpenPeerClient(otherUser.Counter); while (true) { Console.WriteLine("Enter message to send [type 'x' for disconection]:"); string messageToSend = Console.ReadLine(); if (messageToSend.Equals("x") || messageToSend.Equals("X")) { break; } try { proxyPeerClient.SendMessage(messageToSend); }catch (Exception e) { Console.WriteLine("Sending message to client failed. \nDetails: " + e.Message); break; } byte[] encryptedMessage = AES_ENCRYPTION.EncryptFile(messageToSend, decryptedSecretKey); byte[] encryptedSenderName = AES_ENCRYPTION.EncryptFile(senderName, decryptedSecretKey); byte[] encryptedRecieverName = AES_ENCRYPTION.EncryptFile(otherUser.Name, decryptedSecretKey); try { proxyMonitoring.LogMessage(encryptedMessage, encryptedSenderName, encryptedRecieverName); } catch (FaultException e) { Console.WriteLine("Logging message on server failed. \nDetails: " + e.Message); break; } } proxyPeerClient.Abort(); } if (m == 0) { break; } } Console.WriteLine("Press any key to close.."); Console.ReadKey(); try { proxy.Disconnect(winIdentity.User.ToString()); } catch (FaultException e) { Console.WriteLine("Disconnection failed. \nDetails: " + e.Message); } host.Close(); proxy.Close(); return(0); }