///<summary>Sends a request to ODCloudClient.</summary> private static void SendToODCloudClient(ODCloudClientData cloudClientData, CloudClientAction cloudClientAction, Action <string> onReceivedResponse = null) { if (SendDataToBrowser == null) { throw new ODException(nameof(SendDataToBrowser) + " has not been initialized."); } string dataStr = JsonConvert.SerializeObject(cloudClientData); //We will sign dataStr to prove this request came from an Open Dental server. byte[] byteArray = Encoding.Unicode.GetBytes(dataStr); CspParameters csp = new CspParameters { KeyContainerName = "cloudkey", Flags = CspProviderFlags.UseMachineKeyStore, }; using RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp); byte[] signedByteArray = rsa.SignData(byteArray, CryptoConfig.MapNameToOID("SHA256")); string signature = Convert.ToBase64String(signedByteArray); string publicKey = rsa.ToXmlString(false); ODCloudClientArgs jsonData = new ODCloudClientArgs { Data = dataStr, Signature = signature, PublicKey = publicKey, ActionEnum = cloudClientAction, }; string jsonStr = JsonConvert.SerializeObject(jsonData); SendDataToBrowser(jsonStr, (int)BrowserAction.SendToODCloudClient, onReceivedResponse); }
///<summary>Sends a requests to ODCloudClient and waits for a response. Throws any exception that ODCloudClient returns.</summary> private static string SendToODCloudClientSynchronously(ODCloudClientData cloudClientData, CloudClientAction cloudClientAction, int timeoutSecs = 30) { bool hasReceivedResponse = false; string odCloudClientResponse = ""; Exception exFromThread = null; void onReceivedResponse(string response) { hasReceivedResponse = true; odCloudClientResponse = response; } ODThread thread = new ODThread(o => { SendToODCloudClient(cloudClientData, cloudClientAction, onReceivedResponse); }); thread.Name = "SendToODCloudClient"; thread.AddExceptionHandler(e => exFromThread = e); thread.Start(); DateTime start = DateTime.Now; ODProgress.ShowAction(() => { while (!hasReceivedResponse && exFromThread == null && (DateTime.Now - start).TotalSeconds < timeoutSecs) { Thread.Sleep(100); } }); if (exFromThread != null) { throw exFromThread; } if (!hasReceivedResponse) { throw new ODException("Unable to communicate with OD Cloud Client.", ODException.ErrorCodes.ODCloudClientTimeout); } CloudClientResult result = JsonConvert.DeserializeObject <CloudClientResult>(odCloudClientResponse); if (result.ResultCodeEnum == CloudClientResultCode.ODException) { ODException odEx = JsonConvert.DeserializeObject <ODException>(result.ResultData); throw odEx; } if (result.ResultCodeEnum == CloudClientResultCode.Error) { throw new Exception(result.ResultData); } return(result.ResultData); }