Helper for TokenCloudCredentials
        /// <summary>
        /// Return test credentials and URI using AAD auth for an OrgID account.  Use this emthod with causion, it may take a dependency on ADAL
        /// </summary>
        /// <returns>The test credentials, or null if nthe appropriate environment variablke is not set.</returns>
        protected virtual TestEnvironment GetOrgIdTestEnvironment(string orgIdVariable)
        {
            TestEnvironment orgIdEnvironment = null;
            string          orgIdAuth        = GetOrgId(orgIdVariable);

            if (!string.IsNullOrEmpty(orgIdAuth))
            {
                string token = null;
                IDictionary <string, string> authSettings = ParseConnectionString(orgIdAuth);
                string subscription = authSettings[SubscriptionIdKey];
                string authEndpoint = authSettings.ContainsKey(AADAuthEndpoint) ? authSettings[AADAuthEndpoint] : AADAuthEndpointDefault;
                string tenant       = authSettings.ContainsKey(AADTenant) ? authSettings[AADTenant] : AADTenantDefault;
                string user         = null;
                if (authSettings.ContainsKey(AADUserIdKey) && authSettings.ContainsKey(AADPasswordKey))
                {
                    user = authSettings[AADUserIdKey];
                    string password = authSettings[AADPasswordKey];
                    Tracing.Information("Using AAD auth with username and password combination");
                    token = TokenCloudCredentialsHelper.GetTokenFromBasicCredentials(user, password, authEndpoint, tenant);
                    Tracing.Information("Using token {0}", token);
                }
                else
                {
                    Tracing.Information("Using AAD auth with pop-up dialog");
                    string clientId = authSettings.ContainsKey(ClientID) ? authSettings[ClientID] : ClientIdDefault;
                    if (authSettings.ContainsKey(RawToken))
                    {
                        token = authSettings[RawToken];
                    }
                    else
                    {
                        token = TokenCloudCredentialsHelper.GetToken(authEndpoint, tenant, clientId);
                    }
                }

                orgIdEnvironment = new TestEnvironment
                {
                    Credentials = token == null ? null : new TokenCloudCredentials(subscription, token),
                    UserName    = user
                };

                if (authSettings.ContainsKey(BaseUriKey))
                {
                    orgIdEnvironment.BaseUri = new Uri(authSettings[BaseUriKey]);
                }

                if (!string.IsNullOrEmpty(authEndpoint))
                {
                    orgIdEnvironment.ActiveDirectoryEndpoint = new Uri(authEndpoint);
                }

                orgIdEnvironment.SubscriptionId = subscription;
            }

            return(orgIdEnvironment);
        }
        /// <summary>
        /// Return test credentials and URI using AAD auth for an OrgID account.  Use this emthod with causion, it may take a dependency on ADAL
        /// </summary>
        /// <returns>The test credentials, or null if nthe appropriate environment variablke is not set.</returns>
        protected virtual TestEnvironment GetOrgIdTestEnvironment(string orgIdVariable)
        {
            TestEnvironment orgIdEnvironment = null;
            string          orgIdAuth        = GetOrgId(orgIdVariable);

            if (!string.IsNullOrEmpty(orgIdAuth))
            {
                string token = null;
                IDictionary <string, string> authSettings = ParseConnectionString(orgIdAuth);
                string subscription         = authSettings[SubscriptionIdKey];
                string authEndpoint         = authSettings.ContainsKey(AADAuthEndpoint) ? authSettings[AADAuthEndpoint] : AADAuthEndpointDefault;
                string tenant               = authSettings.ContainsKey(AADTenant) ? authSettings[AADTenant] : AADTenantDefault;
                string user                 = null;
                AuthenticationResult result = null;

                if (authSettings.ContainsKey(AADUserIdKey))
                {
                    user = authSettings[AADUserIdKey];
                }

                // Preserve/restore subscription ID
                if (HttpMockServer.Mode == HttpRecorderMode.Record)
                {
                    HttpMockServer.Variables[SubscriptionIdKey] = subscription;
                    if (authSettings.ContainsKey(AADUserIdKey) && authSettings.ContainsKey(AADPasswordKey))
                    {
                        string password = authSettings[AADPasswordKey];
                        TracingAdapter.Information("Using AAD auth with username and password combination");
                        token = TokenCloudCredentialsHelper.GetTokenFromBasicCredentials(user, password, authEndpoint, tenant);
                    }
                    else
                    {
                        TracingAdapter.Information("Using AAD auth with pop-up dialog");
                        string clientId = authSettings.ContainsKey(ClientID) ? authSettings[ClientID] : ClientIdDefault;
                        if (authSettings.ContainsKey(RawToken))
                        {
                            token = authSettings[RawToken];
                        }
                        else
                        {
                            result = TokenCloudCredentialsHelper.GetToken(authEndpoint, tenant, clientId);
                            token  = result.CreateAuthorizationHeader().Substring("Bearer ".Length);
                        }
                    }
                }

                if (HttpMockServer.Mode == HttpRecorderMode.Playback)
                {
                    // playback mode but no stored credentials in mocks
                    TracingAdapter.Information("Using dummy token for playback");
                    token = Guid.NewGuid().ToString();
                }

                orgIdEnvironment = new TestEnvironment
                {
                    Credentials          = token == null ? null : new TokenCloudCredentials(subscription, token),
                    UserName             = user,
                    AuthenticationResult = result
                };

                if (authSettings.ContainsKey(BaseUriKey))
                {
                    orgIdEnvironment.BaseUri = new Uri(authSettings[BaseUriKey]);
                }

                if (!string.IsNullOrEmpty(authEndpoint))
                {
                    orgIdEnvironment.ActiveDirectoryEndpoint = new Uri(authEndpoint);
                }

                orgIdEnvironment.SubscriptionId = subscription;
            }

            return(orgIdEnvironment);
        }