internal static string ExportCertificate(string source, SecureString securePassword, string thumbprint) { if (string.IsNullOrEmpty(source)) { throw new ArgumentNullException("source"); } if (securePassword == null) { throw new ArgumentNullException("securePassword"); } if (string.IsNullOrEmpty(thumbprint)) { throw new ArgumentNullException("thumbprint"); } ExchangeCertificateRpc exchangeCertificateRpc = new ExchangeCertificateRpc(); exchangeCertificateRpc.ExportByThumbprint = thumbprint; exchangeCertificateRpc.ExportBinary = true; ExchangeCertificateRpcVersion exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; byte[] outputBlob = null; try { byte[] inBlob = exchangeCertificateRpc.SerializeInputParameters(ExchangeCertificateRpcVersion.Version2); ExchangeCertificateRpcClient2 exchangeCertificateRpcClient = new ExchangeCertificateRpcClient2(source); outputBlob = exchangeCertificateRpcClient.ExportCertificate2(0, inBlob, securePassword); exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version2; } catch (RpcException) { exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; } if (exchangeCertificateRpcVersion == ExchangeCertificateRpcVersion.Version1) { try { byte[] inBlob2 = exchangeCertificateRpc.SerializeInputParameters(exchangeCertificateRpcVersion); ExchangeCertificateRpcClient exchangeCertificateRpcClient2 = new ExchangeCertificateRpcClient(source); outputBlob = exchangeCertificateRpcClient2.ExportCertificate(0, inBlob2, securePassword); } catch (RpcException e) { ManageExchangeCertificate.ThrowLocalizedException(e, source); } } ExchangeCertificateRpc exchangeCertificateRpc2 = new ExchangeCertificateRpc(exchangeCertificateRpcVersion, null, outputBlob); if (!string.IsNullOrEmpty(exchangeCertificateRpc2.ReturnTaskErrorString)) { throw new InvalidOperationException(exchangeCertificateRpc2.ReturnTaskErrorString); } return(Convert.ToBase64String(exchangeCertificateRpc2.ReturnExportFileData)); }
internal static void ImportCertificate(string destination, SecureString securePassword, string base64cert) { if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException("destination"); } if (securePassword == null) { throw new ArgumentNullException("securePassword"); } if (string.IsNullOrEmpty(base64cert)) { throw new ArgumentNullException("base64cert"); } ExchangeCertificateRpc exchangeCertificateRpc = new ExchangeCertificateRpc(); exchangeCertificateRpc.ImportCert = base64cert; exchangeCertificateRpc.ImportExportable = true; ExchangeCertificateRpcVersion exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; byte[] outputBlob = null; try { byte[] inBlob = exchangeCertificateRpc.SerializeInputParameters(ExchangeCertificateRpcVersion.Version2); ExchangeCertificateRpcClient2 exchangeCertificateRpcClient = new ExchangeCertificateRpcClient2(destination); outputBlob = exchangeCertificateRpcClient.ImportCertificate2(0, inBlob, securePassword); exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version2; } catch (RpcException) { exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; } if (exchangeCertificateRpcVersion == ExchangeCertificateRpcVersion.Version1) { try { byte[] inBlob2 = exchangeCertificateRpc.SerializeInputParameters(exchangeCertificateRpcVersion); ExchangeCertificateRpcClient exchangeCertificateRpcClient2 = new ExchangeCertificateRpcClient(destination); outputBlob = exchangeCertificateRpcClient2.ImportCertificate(0, inBlob2, securePassword); } catch (RpcException e) { ManageExchangeCertificate.ThrowLocalizedException(e, destination); } } ExchangeCertificateRpc exchangeCertificateRpc2 = new ExchangeCertificateRpc(exchangeCertificateRpcVersion, null, outputBlob); if (!string.IsNullOrEmpty(exchangeCertificateRpc2.ReturnTaskErrorString)) { throw new InvalidOperationException(exchangeCertificateRpc2.ReturnTaskErrorString); } }
internal static void EnableCertificateForNetworkService(string destination, string thumbprint) { if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException("destination"); } if (string.IsNullOrEmpty(thumbprint)) { throw new ArgumentNullException("thumbprint"); } ExchangeCertificateRpc exchangeCertificateRpc = new ExchangeCertificateRpc(); exchangeCertificateRpc.EnableByThumbprint = thumbprint; exchangeCertificateRpc.EnableNetworkService = true; exchangeCertificateRpc.EnableServices = AllowedServices.None; ExchangeCertificateRpcVersion exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; byte[] outputBlob = null; try { byte[] inBlob = exchangeCertificateRpc.SerializeInputParameters(ExchangeCertificateRpcVersion.Version2); ExchangeCertificateRpcClient2 exchangeCertificateRpcClient = new ExchangeCertificateRpcClient2(destination); outputBlob = exchangeCertificateRpcClient.EnableCertificate2(0, inBlob); exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version2; } catch (RpcException) { exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1; } if (exchangeCertificateRpcVersion == ExchangeCertificateRpcVersion.Version1) { try { byte[] inBlob2 = exchangeCertificateRpc.SerializeInputParameters(exchangeCertificateRpcVersion); ExchangeCertificateRpcClient exchangeCertificateRpcClient2 = new ExchangeCertificateRpcClient(destination); outputBlob = exchangeCertificateRpcClient2.EnableCertificate(0, inBlob2); } catch (RpcException e) { ManageExchangeCertificate.ThrowLocalizedException(e, destination); } } ExchangeCertificateRpc exchangeCertificateRpc2 = new ExchangeCertificateRpc(exchangeCertificateRpcVersion, null, outputBlob); if (!string.IsNullOrEmpty(exchangeCertificateRpc2.ReturnTaskErrorString)) { throw new InvalidOperationException(exchangeCertificateRpc2.ReturnTaskErrorString); } }