private static string GetUcwaUserResourceUri(HttpClient httpClient, AuthenticationContext authenticationContext, String ucwaUserDiscoveryUri, string clientId,
                                                     string redirectUri, UserCredential uc)
        {
            AuthenticationResult authenticationResult = null;

            authenticationResult = AzureAdAuth.GetAzureAdToken(authenticationContext, ucwaUserDiscoveryUri, clientId, redirectUri, uc);

            string ucwaUserResourceUri = String.Empty;

            httpClient.DefaultRequestHeaders.Clear();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
            var httpResponseMessage = httpClient.GetAsync(ucwaUserDiscoveryUri).Result;

            Console.WriteLine("Called " + ucwaUserDiscoveryUri);
            var resultString = httpResponseMessage.Content.ReadAsStringAsync().Result;

            Console.WriteLine("GetUcwaUserResourceUri Body " + resultString);
            dynamic resultObject        = JObject.Parse(resultString);
            string  resourceRedirectUri = "";

            try
            {
                resourceRedirectUri = resultObject._links.redirect.href;
            }
            catch
            {
                Console.WriteLine("No re-direct");
            }
            if (resourceRedirectUri != "")
            {
                Console.WriteLine("GetUcwaUserResourceUri redirect is " + resourceRedirectUri);
                resourceRedirectUri += "/oauth/user";  // for some reason, the redirectUri doesn't include /oauth/user
                Console.WriteLine("Modifying GetUcwaUserResourceUri to be correct " + resourceRedirectUri);
                // recursion is your friend
                ucwaUserResourceUri = GetUcwaUserResourceUri(httpClient, authenticationContext, resourceRedirectUri, clientId, redirectUri, uc);
            }
            else  // if there's no redirect then the applications URI is there for us to grab
            {
                ucwaUserResourceUri = resultObject._links.applications.href;
            }
            Console.WriteLine("GetUcwaUserResourceUri is " + ucwaUserResourceUri);
            return(ucwaUserResourceUri);
        }
        private static string DoUcwaAutoDiscovery(HttpClient httpClient, AuthenticationContext authenticationContext, String sfboResourceAppId, string clientId, string redirectUri, UserCredential uc)
        {
            AuthenticationResult authenticationResult = null;

            authenticationResult = AzureAdAuth.GetAzureAdToken(authenticationContext, sfboResourceAppId, clientId, redirectUri, uc);

            string ucwaAutoDiscoveryUserRootUri = string.Empty;

            //Console.WriteLine("Using this access token " + result.AccessToken);
            httpClient.DefaultRequestHeaders.Clear();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
            var httpResponseMessage = httpClient.GetAsync(ucwaAutoDiscoveryUri).Result;

            if (httpResponseMessage.IsSuccessStatusCode)
            {
                Console.WriteLine("Called " + ucwaAutoDiscoveryUri);
                var resultString = httpResponseMessage.Content.ReadAsStringAsync().Result;
                Console.WriteLine("DoUcwaDiscovery URI " + resultString);
                dynamic resultObject = JObject.Parse(resultString);
                ucwaAutoDiscoveryUserRootUri = resultObject._links.user.href;
                Console.WriteLine("DoUcwaDiscovery Root URI is " + ucwaAutoDiscoveryUserRootUri);
            }
            return(ucwaAutoDiscoveryUserRootUri);
        }