Exemplo n.º 1
0
        private async Task <InstanceDiscoveryMetadataEntry> FetchNetworkMetadataOrFallbackAsync(
            RequestContext requestContext,
            Uri authorityUri)
        {
            try
            {
                return(await _networkMetadataProvider.GetMetadataAsync(authorityUri, requestContext).ConfigureAwait(false));
            }
            catch (MsalServiceException ex)
            {
                if (!requestContext.ServiceBundle.Config.AuthorityInfo.ValidateAuthority)
                {
                    requestContext.Logger.Info("[Instance Discovery] Skipping Instance discovery as validate authority is set to false.");
                    return(CreateEntryForSingleAuthority(authorityUri));
                }

                // Validate Authority exception
                if (ex.ErrorCode == MsalError.InvalidInstance)
                {
                    throw;
                }

                string message =
                    "[Instance Discovery] Instance Discovery failed. Potential cause: no network connection or discovery endpoint is busy. See exception below. MSAL will continue without network instance metadata.";

                requestContext.Logger.WarningPii(message + " Authority: " + authorityUri, message);
                requestContext.Logger.WarningPii(ex);

                return(_knownMetadataProvider.GetMetadata(authorityUri.Host, Enumerable.Empty <string>(), requestContext.Logger));
            }
        }
Exemplo n.º 2
0
        public async Task NetworkProviderIsCalledLastAsync()
        {
            // Arrange
            _discoveryManager = new InstanceDiscoveryManager(
                _harness.HttpManager,
                _harness.ServiceBundle.MatsTelemetryManager,
                false,
                null,
                null,
                _knownMetadataProvider,
                _networkCacheMetadataProvider,
                _networkMetadataProvider);

            var otherEnvs    = new[] { "env1", "env2" };
            var authorityUri = new Uri(Authority);

            // No response from the static and known provider
            _knownMetadataProvider
            .GetMetadata("some_env.com", otherEnvs, Arg.Any <ICoreLogger>())
            .Returns((InstanceDiscoveryMetadataEntry)null);

            _networkMetadataProvider
            .GetMetadataAsync(authorityUri, _testRequestContext)
            .Returns(_expectedResult);

            // Act
            InstanceDiscoveryMetadataEntry actualResult = await _discoveryManager.GetMetadataEntryTryAvoidNetworkAsync(
                "https://some_env.com/tid",
                otherEnvs,
                _testRequestContext)
                                                          .ConfigureAwait(false);

            // Assert
            Assert.AreSame(_expectedResult, actualResult, "The known metadata provider should be queried second");
            _knownMetadataProvider.Received(1).GetMetadata("some_env.com", otherEnvs, Arg.Any <ICoreLogger>());
            await _networkMetadataProvider.Received(1).GetMetadataAsync(authorityUri, _testRequestContext).ConfigureAwait(false);
        }