public static void AddNamedOAuth2ClientCredentialProvider(this IServiceCollection serviceCollection, OAuth2ClientCredentialConfiguration oAuth2ClientCredentialConfiguration, string name)
        {
            EnsureArg.IsNotNull(serviceCollection, nameof(serviceCollection));
            EnsureArg.IsNotNull(oAuth2ClientCredentialConfiguration, nameof(oAuth2ClientCredentialConfiguration));
            EnsureArg.IsNotNullOrWhiteSpace(name, nameof(name));

            serviceCollection.Add(provider =>
            {
                var options            = Options.Create(oAuth2ClientCredentialConfiguration);
                var httpClient         = new HttpClient();
                var credentialProvider = new OAuth2ClientCredentialProvider(options, httpClient);
                return(new NamedCredentialProvider(name, credentialProvider));
            })
            .Singleton()
            .AsService <NamedCredentialProvider>();
        }
        public DicomWebClient GetDicomWebClient(TestApplication clientApplication)
        {
            EnsureArg.IsNotNull(clientApplication, nameof(clientApplication));
            HttpMessageHandler messageHandler = TestDicomWebServer.CreateMessageHandler();

            if (AuthenticationSettings.SecurityEnabled && !clientApplication.Equals(TestApplications.InvalidClient))
            {
                if (_authenticationHandlers.ContainsKey(clientApplication.ClientId))
                {
                    messageHandler = _authenticationHandlers[clientApplication.ClientId];
                }
                else
                {
                    var credentialConfiguration = new OAuth2ClientCredentialConfiguration(
                        AuthenticationSettings.TokenUri,
                        AuthenticationSettings.Resource,
                        AuthenticationSettings.Scope,
                        clientApplication.ClientId,
                        clientApplication.ClientSecret);
                    var credentialProvider = new OAuth2ClientCredentialProvider(Options.Create(credentialConfiguration), new HttpClient(messageHandler));
                    var authHandler        = new AuthenticationHttpMessageHandler(credentialProvider)
                    {
                        InnerHandler = messageHandler,
                    };

                    _authenticationHandlers.Add(clientApplication.ClientId, authHandler);
                    messageHandler = authHandler;
                }
            }

            var httpClient = new HttpClient(messageHandler)
            {
                BaseAddress = TestDicomWebServer.BaseAddress
            };

            var dicomWebClient = new DicomWebClient(httpClient)
            {
                GetMemoryStream = () => RecyclableMemoryStreamManager.GetStream(),
            };

            return(dicomWebClient);
        }