private static string GetInteractiveClientToken(PACClientInfo clientInfo, PromptBehavior behavior) { // Dummy endpoint just to get unauthorized response var client = new HttpClient(); var query = $"{clientInfo.ServiceUrl}/api/status/4799049A-E623-4B2A-818A-3A674E106DE5"; var request = new HttpRequestMessage(HttpMethod.Get, new Uri(query)); using (var response = client.SendAsync(request).GetAwaiter().GetResult()) { if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { // Method below found here: https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/Acquiring-tokens-interactively---Public-client-application-flows var authParams = AuthenticationParameters.CreateFromUnauthorizedResponseAsync(response).GetAwaiter().GetResult(); var authContext = new AuthenticationContext(authParams.Authority); var authResult = authContext.AcquireTokenAsync( resourceUrl, clientInfo.ClientId.ToString(), new Uri(redirectUrl), new PlatformParameters(behavior)).GetAwaiter().GetResult(); return(authResult.AccessToken); } else { throw new Exception($"Unable to connect to the service for authorization information. {response.ReasonPhrase}"); } } }
private static async Task <string> GetAuthorityForSubscription(string subscriptionId) { var url = $"{ManagementEndpoint}/subscriptions/{subscriptionId}?api-version=2014-04-01"; using (var client = new HttpClient()) { var response = await client.GetAsync(url); var authenticationParameters = await AuthenticationParameters.CreateFromUnauthorizedResponseAsync(response); return(authenticationParameters.Authority); } }