private static HttpStatusCode UploadNewManagementCert(string subscriptionId, X509Certificate2 existingCertificate, string newCertificateCerFilePath) { var statusCode = HttpStatusCode.Unused; var newCertificate = new X509Certificate2(newCertificateCerFilePath); var creds = new CertificateCloudCredentials(subscriptionId, existingCertificate); var client = new ManagementClient(creds); var parm = new ManagementCertificateCreateParameters() { Data = newCertificate.RawData, PublicKey = newCertificate.GetPublicKey(), Thumbprint = newCertificate.Thumbprint }; //Hyak throws an exception for a Created result, which is actually the success code try { var response = client.ManagementCertificates.Create(parm); } catch (CloudException ex) { statusCode = ex.Response.StatusCode; } return(statusCode); }
/// <summary> /// The Create Management Certificate operation adds a certificate to /// the list of management certificates. Management certificates, /// which are also known as subscription certificates, authenticate /// clients attempting to connect to resources associated with your /// Azure subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj154123.aspx /// for more information) /// </summary> /// <param name='parameters'> /// Required. Parameters supplied to the Create Management Certificate /// operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public async Task <AzureOperationResponse> CreateAsync(ManagementCertificateCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (parameters == null) { throw new ArgumentNullException("parameters"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("parameters", parameters); TracingAdapter.Enter(invocationId, this, "CreateAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/certificates"; string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Post; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2014-10-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; XDocument requestDoc = new XDocument(); XElement subscriptionCertificateElement = new XElement(XName.Get("SubscriptionCertificate", "http://schemas.microsoft.com/windowsazure")); requestDoc.Add(subscriptionCertificateElement); if (parameters.PublicKey != null) { XElement subscriptionCertificatePublicKeyElement = new XElement(XName.Get("SubscriptionCertificatePublicKey", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificatePublicKeyElement.Value = Convert.ToBase64String(parameters.PublicKey); subscriptionCertificateElement.Add(subscriptionCertificatePublicKeyElement); } if (parameters.Thumbprint != null) { XElement subscriptionCertificateThumbprintElement = new XElement(XName.Get("SubscriptionCertificateThumbprint", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificateThumbprintElement.Value = parameters.Thumbprint; subscriptionCertificateElement.Add(subscriptionCertificateThumbprintElement); } if (parameters.Data != null) { XElement subscriptionCertificateDataElement = new XElement(XName.Get("SubscriptionCertificateData", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificateDataElement.Value = Convert.ToBase64String(parameters.Data); subscriptionCertificateElement.Add(subscriptionCertificateDataElement); } requestContent = requestDoc.ToString(); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml"); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result AzureOperationResponse result = null; // Deserialize Response result = new AzureOperationResponse(); result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
/// <summary> /// The Add Management Certificate operation adds a certificate to the /// list of management certificates. Management certificates, which /// are also known as subscription certificates, authenticate clients /// attempting to connect to resources associated with your Windows /// Azure subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj154123.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.IManagementCertificateOperations. /// </param> /// <param name='parameters'> /// Parameters supplied to the Create Management Certificate operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse Create(this IManagementCertificateOperations operations, ManagementCertificateCreateParameters parameters) { try { return(operations.CreateAsync(parameters).Result); } catch (AggregateException ex) { if (ex.InnerExceptions.Count > 1) { throw; } else { throw ex.InnerException; } } }
/// <summary> /// The Add Management Certificate operation adds a certificate to the /// list of management certificates. Management certificates, which /// are also known as subscription certificates, authenticate clients /// attempting to connect to resources associated with your Windows /// Azure subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj154123.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.IManagementCertificateOperations. /// </param> /// <param name='parameters'> /// Parameters supplied to the Create Management Certificate operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static Task <OperationResponse> CreateAsync(this IManagementCertificateOperations operations, ManagementCertificateCreateParameters parameters) { return(operations.CreateAsync(parameters, CancellationToken.None)); }
/// <summary> /// The Create Management Certificate operation adds a certificate to /// the list of management certificates. Management certificates, /// which are also known as subscription certificates, authenticate /// clients attempting to connect to resources associated with your /// Azure subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj154123.aspx /// for more information) /// </summary> /// <param name='operations'> /// Reference to the /// Microsoft.WindowsAzure.Management.IManagementCertificateOperations. /// </param> /// <param name='parameters'> /// Required. Parameters supplied to the Create Management Certificate /// operation. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public static OperationResponse Create(this IManagementCertificateOperations operations, ManagementCertificateCreateParameters parameters) { return(Task.Factory.StartNew((object s) => { return ((IManagementCertificateOperations)s).CreateAsync(parameters); } , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult()); }
/// <summary> /// The Add Management Certificate operation adds a certificate to the /// list of management certificates. Management certificates, which /// are also known as subscription certificates, authenticate clients /// attempting to connect to resources associated with your Windows /// Azure subscription. (see /// http://msdn.microsoft.com/en-us/library/windowsazure/jj154123.aspx /// for more information) /// </summary> /// <param name='parameters'> /// Parameters supplied to the Create Management Certificate operation. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// A standard service response including an HTTP status code and /// request ID. /// </returns> public async System.Threading.Tasks.Task <OperationResponse> CreateAsync(ManagementCertificateCreateParameters parameters, CancellationToken cancellationToken) { // Validate if (parameters == null) { throw new ArgumentNullException("parameters"); } // Tracing bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("parameters", parameters); Tracing.Enter(invocationId, this, "CreateAsync", tracingParameters); } // Construct URL string url = new Uri(this.Client.BaseUri, "/").ToString() + this.Client.Credentials.SubscriptionId + "/certificates"; // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Post; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2013-03-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; XDocument requestDoc = new XDocument(); XElement subscriptionCertificateElement = new XElement(XName.Get("SubscriptionCertificate", "http://schemas.microsoft.com/windowsazure")); requestDoc.Add(subscriptionCertificateElement); if (parameters.PublicKey != null) { XElement subscriptionCertificatePublicKeyElement = new XElement(XName.Get("SubscriptionCertificatePublicKey", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificatePublicKeyElement.Value = Convert.ToBase64String(parameters.PublicKey); subscriptionCertificateElement.Add(subscriptionCertificatePublicKeyElement); } if (parameters.Thumbprint != null) { XElement subscriptionCertificateThumbprintElement = new XElement(XName.Get("SubscriptionCertificateThumbprint", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificateThumbprintElement.Value = parameters.Thumbprint; subscriptionCertificateElement.Add(subscriptionCertificateThumbprintElement); } if (parameters.Data != null) { XElement subscriptionCertificateDataElement = new XElement(XName.Get("SubscriptionCertificateData", "http://schemas.microsoft.com/windowsazure")); subscriptionCertificateDataElement.Value = Convert.ToBase64String(parameters.Data); subscriptionCertificateElement.Add(subscriptionCertificateDataElement); } requestContent = requestDoc.ToString(); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { Tracing.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Xml); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result OperationResponse result = null; result = new OperationResponse(); result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }