/// <summary> /// Verify accquired graph token /// </summary> private void VerifyGraphToken() { if (this.TokenInfo[TokenAudience.Graph] != null) { try { string operationUrl = string.Format(@"{0}{1}/users?$orderby=displayName&$top=25&api-version=1.6", this.GraphUri, this.Tenant); TokenCredentials graphCredential = this.TokenInfo[TokenAudience.Graph]; var request = new HttpRequestMessage { RequestUri = new Uri(operationUrl) }; HttpClient client = new HttpClient(); graphCredential.ProcessHttpRequestAsync(request, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); HttpResponseMessage response = client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); string jsonString = response.Content.ReadAsStringAsync().Result; var jsonResult = JObject.Parse(jsonString); } catch (Exception ex) { string graphFailMessage = string.Format(@"Unable to make request to graph endpoint '{0}' using credentials provided within the connectionstring", this.GraphUri); Debug.WriteLine(graphFailMessage, ex.ToString()); } } }
public MicrosoftGraphServiceClient( TokenCredentials microsoftGraphTokenCredentials, CancellationToken cancellationToken = default ) { var delegateAuthenticationProvider = new DelegateAuthenticationProvider( async(httpRequestMessage) => { await microsoftGraphTokenCredentials .ProcessHttpRequestAsync( httpRequestMessage, cancellationToken ); } ); _graphServiceClient = new GraphServiceClient(delegateAuthenticationProvider); }
/// <summary> /// Retrieve list of subscription for current user /// </summary> /// <param name="baseuri"></param> /// <param name="credentials"></param> /// <returns></returns> private List <SubscriptionInfo> ListSubscriptions(string baseuri, TokenCredentials credentials) { var request = new HttpRequestMessage { RequestUri = new Uri(string.Format("{0}/subscriptions?api-version=2014-04-01-preview", baseuri)) }; HttpClient client = new HttpClient(); credentials.ProcessHttpRequestAsync(request, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); HttpResponseMessage response = client.SendAsync(request).Result; response.EnsureSuccessStatusCode(); string jsonString = response.Content.ReadAsStringAsync().Result; var jsonResult = JObject.Parse(jsonString); var results = ((JArray)jsonResult["value"]).Select(item => new SubscriptionInfo((JObject)item)).ToList(); return(results); }
public async Task DeleteResourceGroups() { HttpClient httpClient = new HttpClient(); foreach (var resourceGroupUri in _resourceGroupsCreated) { HttpRequestMessage httpRequest = new HttpRequestMessage(); httpRequest.Method = new HttpMethod("DELETE"); httpRequest.RequestUri = new Uri(resourceGroupUri); _tokenCredentials.ProcessHttpRequestAsync(httpRequest, CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest).ConfigureAwait(false); string groupName = _resourceGroupPattern.Match(resourceGroupUri).Groups[1].Value; string message = string.Format(CultureInfo.InvariantCulture, "Started deletion of resource group '{0}'. Server responded with status code {1}.", groupName, httpResponse.StatusCode); Console.WriteLine(message); Debug.WriteLine(message); } }