private async Task <HttpResponseMessage> HttpRegistration(string uri, RegistrationInfo info) { HttpResponseMessage response = await client.PostAsJsonAsync(uri, info); return(response); }
public async Task <string> Register(string username, string password, string vmname, string malwareX, string malwareY) { string responseCode = ""; if (!UserIsValid(username, password)) { throw new UnauthorizedAccessException("Invalid credentials supplied"); } RegistryKey mfkey = RegKeyFromPath(@"HKEY_LOCAL_MACHINE\SOFTWARE\" + manufacturer, true); if (mfkey == null) { RegistryKey baseKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true); mfkey = baseKey.CreateSubKey(manufacturer); } if (!mfkey.GetValueNames().Contains("Registered")) { // perform registration string uri = "register"; eventLog1.WriteEntry($"Connecting to registration API at {uri.ToString()}", System.Diagnostics.EventLogEntryType.Information, 104); RegistrationInfo ri = new RegistrationInfo(); OfficeInfo oi = new OfficeInfo(); ri.GUID = guid; ri.VMName = vmname; ri.OfficeVersionString = oi.version; ri.OfficeVersionNum = oi.vnum; ri.username = username; ri.password = password; ri.MalwarePosX = Int32.Parse(malwareX); ri.MalwarePosY = Int32.Parse(malwareY); ri.VMIP = FindIP().ToString(); // get screen resolution ri.DisplayWidth = Screen.PrimaryScreen.Bounds.Width; ri.DisplayHeight = Screen.PrimaryScreen.Bounds.Height; // get OS version name var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast <ManagementObject>() select x.GetPropertyValue("Caption")).FirstOrDefault(); if (name != null) { ri.OSName = name.ToString(); } // record successful registration HttpResponseMessage registrationResult = await HttpRegistration(uri, ri); responseCode = registrationResult.StatusCode.ToString(); if (registrationResult.IsSuccessStatusCode) { mfkey.SetValue("Registered", 1); registered = true; eventLog1.WriteEntry($"Registered with API with status {responseCode}", System.Diagnostics.EventLogEntryType.SuccessAudit, 100); } else { string errstr = registrationResult.ToString(); eventLog1.WriteEntry($"Registration failed with error {errstr}", System.Diagnostics.EventLogEntryType.FailureAudit, 105); throw new ApplicationException($"Registration failure with code {errstr}"); } } else { // otherwise registration is complete registered = true; } return(responseCode); }