public static void BackupSmartcard() { BrowserHelperObject.log("BHO", "BackupSmartcard", "BackupSmartcard - start"); long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; var sessionID = "BackupSmartcardIE" + milliseconds; ResultObject check = CheckSmartCard(sessionID); if (check != null) { MessageBox.Show("Backup Smartcard - Failed : " + check.message, "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } HttpWebResponse abcResponse; var abcRequest = (HttpWebRequest)WebRequest.Create(USER_ABCE_SERVICE + "/user/backupExists/" + sessionID); abcRequest.ContentType = "text/plain"; abcRequest.Method = "GET"; try { abcResponse = (HttpWebResponse)abcRequest.GetResponse(); if (abcResponse.StatusCode == HttpStatusCode.NoContent) { BrowserHelperObject.log("BHO", "BackupSmartcard", " - ' no content ' backup file exist!"); var result = MessageBox.Show("Backup file already exists. Pressing ok will overwrite it. Are you sure you want to continue?", "Backup Smartcard", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); if (result != System.Windows.Forms.DialogResult.Yes) { return; } } } catch (WebException e) { MessageBox.Show("Check if backup file existe failed : " + e, "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } PasswordForm pf = new PasswordForm(); var resPf = pf.ShowDialog(); if(resPf == System.Windows.Forms.DialogResult.Cancel) { return; } string password = pf.getPassword(); if(password.Length != 8) { MessageBox.Show("Password must be 8 chars", "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } // unlock System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] passwordBytes = encoding.GetBytes(password); abcRequest = (HttpWebRequest)WebRequest.Create(USER_ABCE_SERVICE + "/user/backupSmartcard/" + sessionID); abcRequest.ContentType = "text/plain"; abcRequest.Method = "POST"; try { var abcRequestStream = abcRequest.GetRequestStream(); BrowserHelperObject.log("BHO", "BackupSmartcard", " - rest call 'backupSmartcard'"); abcRequestStream.Write(passwordBytes, 0, passwordBytes.Length); abcRequestStream.Close(); abcRequestStream.Dispose(); abcResponse = (HttpWebResponse)abcRequest.GetResponse(); } catch (WebException e) { BrowserHelperObject.log("BHO", "BackupSmartcard", " - Error calling 'backupSmartcard' " + e); MessageBox.Show("Something went wrong during backup! " + e, "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } BrowserHelperObject.log("BHO", "BackupSmartcard", " - Success calling 'backupSmartcard' "); MessageBox.Show("Card backed up successfully", "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; }
internal static void RestoreSmartcard() { BrowserHelperObject.log("BHO", "RestoreSmartcard", "RestoreSmartcard- start"); long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; var sessionID = "RestoreSmartcardIE" + milliseconds; ResultObject check = CheckSmartCard(sessionID); if (check != null) { MessageBox.Show("Restore Smartcard - Failed : " + check.message, "Restore Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } HttpWebResponse abcResponse; PasswordForm pf = new PasswordForm(); var resPf = pf.ShowDialog(); if (resPf == System.Windows.Forms.DialogResult.Cancel) { return; } string password = pf.getPassword(); if (password.Length != 8) { MessageBox.Show("Password must be 8 chars", "Backup Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } // unlock System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); Byte[] passwordBytes = encoding.GetBytes(password); var abcRequest = (HttpWebRequest)WebRequest.Create(USER_ABCE_SERVICE + "/user/restoreSmartcard/" + sessionID); abcRequest.ContentType = "text/plain"; abcRequest.Method = "POST"; try { var abcRequestStream = abcRequest.GetRequestStream(); BrowserHelperObject.log("BHO", "RestoreSmartcard", " - rest call 'restoreSmartcard'"); abcRequestStream.Write(passwordBytes, 0, passwordBytes.Length); abcRequestStream.Close(); abcRequestStream.Dispose(); abcResponse = (HttpWebResponse)abcRequest.GetResponse(); } catch (WebException e) { BrowserHelperObject.log("BHO", "RestoreSmartcard", " - Error calling 'restoreSmartcard' " + e); MessageBox.Show("Something went wrong during restoration! " + e, "Restore Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; } BrowserHelperObject.log("BHO", "RestoreSmartcard", " - Success calling 'restoreSmartcard' "); MessageBox.Show("Card restored successfully!", "Restore Smartcard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); return; }