/// <summary> /// Submits the http delete request to the specified uri. /// </summary> /// <param name="uri">The uri to which the delete request will be issued.</param> /// <param name="allowRetry">Allow the Post to be retried after issuing a Get call. Currently used for CSRF failures.</param> /// <returns>Task tracking HTTP completion</returns> #pragma warning disable 1998 private async Task <Stream> Delete(Uri uri, bool allowRetry = true) { IBuffer dataBuffer = null; HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); httpFilter.AllowUI = false; if (this.deviceConnection.Credentials != null) { httpFilter.ServerCredential = new PasswordCredential(); httpFilter.ServerCredential.UserName = this.deviceConnection.Credentials.UserName; httpFilter.ServerCredential.Password = this.deviceConnection.Credentials.Password; } using (HttpClient client = new HttpClient(httpFilter)) { this.ApplyHttpHeaders(client, HttpMethods.Delete); IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> responseOperation = client.DeleteAsync(uri); TaskAwaiter <HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter(); while (!responseAwaiter.IsCompleted) { } using (HttpResponseMessage response = responseOperation.GetResults()) { if (!response.IsSuccessStatusCode) { // If this isn't a retry and it failed due to a bad CSRF // token, issue a GET to refresh the token and then retry. if (allowRetry && this.IsBadCsrfToken(response)) { await this.RefreshCsrfToken(); return(await this.Delete(uri, false)); } throw new DevicePortalException(response); } this.RetrieveCsrfToken(response); if (response.Content != null) { using (IHttpContent messageContent = response.Content) { IAsyncOperationWithProgress <IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync(); while (bufferOperation.Status != AsyncStatus.Completed) { } dataBuffer = bufferOperation.GetResults(); } } } } return((dataBuffer != null) ? dataBuffer.AsStream() : null); }
/// <summary> /// Submits the http post request to the specified uri. /// </summary> /// <param name="uri">The uri to which the post request will be issued.</param> /// <param name="requestStream">Optional stream containing data for the request body.</param> /// <param name="requestStreamContentType">The type of that request body data.</param> /// <returns>Task tracking the completion of the POST request</returns> #pragma warning disable 1998 private async Task <Stream> PostAsync( Uri uri, Stream requestStream = null, string requestStreamContentType = null) { HttpStreamContent requestContent = null; IBuffer dataBuffer = null; if (requestStream != null) { requestContent = new HttpStreamContent(requestStream.AsInputStream()); requestContent.Headers.Remove(ContentTypeHeaderName); requestContent.Headers.TryAppendWithoutValidation(ContentTypeHeaderName, requestStreamContentType); } HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); httpFilter.AllowUI = false; if (this.deviceConnection.Credentials != null) { httpFilter.ServerCredential = new PasswordCredential(); httpFilter.ServerCredential.UserName = this.deviceConnection.Credentials.UserName; httpFilter.ServerCredential.Password = this.deviceConnection.Credentials.Password; } using (HttpClient client = new HttpClient(httpFilter)) { this.ApplyHttpHeaders(client, HttpMethods.Post); IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> responseOperation = client.PostAsync(uri, requestContent); TaskAwaiter <HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter(); while (!responseAwaiter.IsCompleted) { } using (HttpResponseMessage response = responseOperation.GetResults()) { if (!response.IsSuccessStatusCode) { throw new DevicePortalException(response); } if (response.Content != null) { using (IHttpContent messageContent = response.Content) { IAsyncOperationWithProgress <IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync(); while (bufferOperation.Status != AsyncStatus.Completed) { } dataBuffer = bufferOperation.GetResults(); } } } } return((dataBuffer != null) ? dataBuffer.AsStream() : null); }
/// <summary> /// Submits the http put request to the specified uri. /// </summary> /// <param name="uri">The uri to which the put request will be issued.</param> /// <param name="body">The HTTP content comprising the body of the request.</param> /// <returns>Task tracking the PUT completion.</returns> #pragma warning disable 1998 private async Task <Stream> PutAsync( Uri uri, IHttpContent body = null) { IBuffer dataBuffer = null; HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); httpFilter.AllowUI = false; if (this.deviceConnection.Credentials != null) { httpFilter.ServerCredential = new PasswordCredential(); httpFilter.ServerCredential.UserName = this.deviceConnection.Credentials.UserName; httpFilter.ServerCredential.Password = this.deviceConnection.Credentials.Password; } using (HttpClient client = new HttpClient(httpFilter)) { this.ApplyHttpHeaders(client, HttpMethods.Put); // Send the request IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> responseOperation = client.PutAsync(uri, null); TaskAwaiter <HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter(); while (!responseAwaiter.IsCompleted) { } using (HttpResponseMessage response = responseOperation.GetResults()) { if (!response.IsSuccessStatusCode) { throw new DevicePortalException(response); } if (response.Content != null) { using (IHttpContent messageContent = response.Content) { IAsyncOperationWithProgress <IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync(); while (bufferOperation.Status != AsyncStatus.Completed) { } dataBuffer = bufferOperation.GetResults(); } } } } return((dataBuffer != null) ? dataBuffer.AsStream() : null); }
/// <summary> /// Gets the root certificate from the device. /// </summary> /// <param name="acceptUntrustedCerts">Whether or not we should accept untrusted certificates.</param> /// <returns>The device certificate.</returns> #pragma warning disable 1998 public async Task <Certificate> GetRootDeviceCertificate(bool acceptUntrustedCerts = false) { Certificate certificate = null; Uri uri = Utilities.BuildEndpoint(this.deviceConnection.Connection, RootCertificateEndpoint); HttpBaseProtocolFilter requestSettings = new HttpBaseProtocolFilter(); requestSettings.AllowUI = false; if (acceptUntrustedCerts) { requestSettings.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); } using (HttpClient client = new HttpClient(requestSettings)) { this.ApplyHttpHeaders(client, HttpMethods.Get); IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> responseOperation = client.GetAsync(uri); TaskAwaiter <HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter(); while (!responseAwaiter.IsCompleted) { } using (HttpResponseMessage response = responseOperation.GetResults()) { this.RetrieveCsrfToken(response); using (IHttpContent messageContent = response.Content) { IAsyncOperationWithProgress <IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync(); TaskAwaiter <IBuffer> readBufferAwaiter = bufferOperation.GetAwaiter(); while (!readBufferAwaiter.IsCompleted) { } certificate = new Certificate(bufferOperation.GetResults()); } } } return(certificate); }
/// <summary> /// Gets the root certificate from the device. /// </summary> /// <returns>The device certificate.</returns> #pragma warning disable 1998 private async Task <Certificate> GetDeviceCertificate() { Certificate certificate = null; bool useHttps = true; // try https then http while (true) { Uri uri = null; if (useHttps) { uri = Utilities.BuildEndpoint(this.deviceConnection.Connection, RootCertificateEndpoint); } else { Uri baseUri = new Uri(string.Format("http://{0}", this.deviceConnection.Connection.Authority)); uri = Utilities.BuildEndpoint(baseUri, RootCertificateEndpoint); } try { HttpBaseProtocolFilter requestSettings = new HttpBaseProtocolFilter(); requestSettings.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted); requestSettings.AllowUI = false; using (HttpClient client = new HttpClient(requestSettings)) { this.ApplyHttpHeaders(client, HttpMethods.Get); IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> responseOperation = client.GetAsync(uri); TaskAwaiter <HttpResponseMessage> responseAwaiter = responseOperation.GetAwaiter(); while (!responseAwaiter.IsCompleted) { } using (HttpResponseMessage response = responseOperation.GetResults()) { this.RetrieveCsrfToken(response); using (IHttpContent messageContent = response.Content) { IAsyncOperationWithProgress <IBuffer, ulong> bufferOperation = messageContent.ReadAsBufferAsync(); TaskAwaiter <IBuffer> readBufferAwaiter = bufferOperation.GetAwaiter(); while (!readBufferAwaiter.IsCompleted) { } certificate = new Certificate(bufferOperation.GetResults()); if (!certificate.Issuer.Contains(DevicePortalCertificateIssuer)) { certificate = null; throw new DevicePortalException( (HttpStatusCode)0, "Invalid certificate issuer", uri, "Failed to get the device certificate"); } } } } return(certificate); } catch (Exception e) { if (useHttps) { useHttps = false; } else { throw e; } } } }