public HomeController(
     ITokenAcquisition tokenAcquisition,
     IDownstreamWebApi downstreamWebApi)
 {
     _tokenAcquisition = tokenAcquisition;
     _downstreamWebApi = downstreamWebApi;
 }
        /// <summary>
        /// Calls the web API with an HttpPost, providing strongly typed input and getting
        /// strongly typed output.
        /// </summary>
        /// <typeparam name="TOutput">Output type.</typeparam>
        /// <typeparam name="TInput">Input type.</typeparam>
        /// <param name="downstreamWebApi">The downstream web API.</param>
        /// <param name="serviceName">Name of the service describing the downstream web API. There can
        /// be several configuration named sections mapped to a <see cref="DownstreamWebApiOptions"/>,
        /// each for one downstream web API. You can pass-in null, but in that case <paramref name="downstreamWebApiOptionsOverride"/>
        /// needs to be set.</param>
        /// <param name="relativePath">Path to the API endpoint relative to the base URL specified in the configuration.</param>
        /// <param name="inputData">Input data sent to the API.</param>
        /// <param name="downstreamWebApiOptionsOverride">Overrides the options proposed in the configuration described
        /// by <paramref name="serviceName"/>.</param>
        /// <param name="user">[Optional] Claims representing a user. This is useful in platforms like Blazor
        /// or Azure Signal R, where the HttpContext is not available. In other platforms, the library
        /// will find the user from the HttpContext.</param>
        /// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
        /// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
        /// <returns>A strongly typed response from the web API.</returns>
        public static async Task <TOutput?> PostForUserAsync <TOutput, TInput>(
            this IDownstreamWebApi downstreamWebApi,
            string serviceName,
            string relativePath,
            TInput inputData,
            Action <DownstreamWebApiOptions>?downstreamWebApiOptionsOverride = null,
            ClaimsPrincipal?user        = null,
            string?authenticationScheme = null)
            where TOutput : class
        {
            if (downstreamWebApi is null)
            {
                throw new ArgumentNullException(nameof(downstreamWebApi));
            }

            using StringContent? input = ConvertFromInput(inputData);

            HttpResponseMessage response = await downstreamWebApi.CallWebApiForUserAsync(
                serviceName,
                authenticationScheme,
                PrepareOptions(relativePath, downstreamWebApiOptionsOverride, HttpMethod.Post),
                user,
                input).ConfigureAwait(false);

            return(await ConvertToOutput <TOutput>(response).ConfigureAwait(false));
        }
Example #3
0
 public SampleFunc(ILogger <SampleFunc> logger,
                   GraphServiceClient graphServiceClient,
                   IDownstreamWebApi downstreamWebApi)
 {
     _graphServiceClient = graphServiceClient;
     _downstreamWebApi   = downstreamWebApi;
     _logger             = logger;
 }
Example #4
0
 public WeatherForecastController(
     IDownstreamWebApi downstreamWebApi,
     ITokenAcquisition tokenAcquisition,
     GraphServiceClient graphServiceClient)
 {
     _downstreamWebApi   = downstreamWebApi;
     _tokenAcquisition   = tokenAcquisition;
     _graphServiceClient = graphServiceClient;
 }
Example #5
0
        public void AddDownstreamWebApiOptionsTest(bool useDownstreamWebApiOptions)
        {
            Action <DownstreamWebApiOptions> configureDownstreamWebApiOptions = (options) =>
            {
                options.BaseUrl = TestConstants.GraphBaseUrlBeta;
                options.Scopes  = TestConstants.Scopes;
                options.Tenant  = TestConstants.TenantIdAsGuid;
            };

            var configMock = Substitute.For <IConfiguration>();

            configMock.Configure().GetSection(ConfigSectionName).Returns(_configSection);

            var services = new ServiceCollection();
            var builder  = services.AddAuthentication()
                           .AddMicrosoftIdentityWebApp(configMock, ConfigSectionName)
                           .EnableTokenAcquisitionToCallDownstreamApi()
                           .AddInMemoryTokenCaches();

            if (useDownstreamWebApiOptions)
            {
                builder.AddDownstreamWebApi(ConfigSectionName, configureDownstreamWebApiOptions);
            }
            else
            {
                builder.AddDownstreamWebApi(ConfigSectionName, configMock);
            }

            var provider = services.BuildServiceProvider();

            // Assert correct services added
            Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions <OpenIdConnectOptions>));
            Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions <MicrosoftIdentityOptions>));
            Assert.Contains(services, s => s.ServiceType == typeof(IConfigureOptions <DownstreamWebApiOptions>));

            // Assert properties set
            var downstreamWebApiOptions        = provider.GetRequiredService <IOptionsSnapshot <DownstreamWebApiOptions> >();
            IDownstreamWebApi downstreamWebApi = provider.GetRequiredService <IDownstreamWebApi>();

            if (useDownstreamWebApiOptions)
            {
                Assert.Equal(TestConstants.GraphBaseUrlBeta, downstreamWebApiOptions.Get(ConfigSectionName).BaseUrl);
                Assert.Equal(TestConstants.Scopes, downstreamWebApiOptions.Get(ConfigSectionName).Scopes);
                Assert.Equal(TestConstants.TenantIdAsGuid, downstreamWebApiOptions.Get(ConfigSectionName).Tenant);
            }
            else
            {
                Assert.Equal(Constants.GraphBaseUrlV1, downstreamWebApiOptions.Get(ConfigSectionName).BaseUrl);
                Assert.Null(downstreamWebApiOptions.Get(ConfigSectionName).Scopes);
                Assert.Null(downstreamWebApiOptions.Get(ConfigSectionName).Tenant);
            }
        }
Example #6
0
        /// <summary>
        /// Call a web API with a strongly typed input, with an HttpGet.
        /// </summary>
        /// <typeparam name="TInput">Input type.</typeparam>
        /// <param name="downstreamWebApi">The downstream web API.</param>
        /// <param name="serviceName">Name of the service describing the downstream web API. There can
        /// be several configuration named sections mapped to a <see cref="DownstreamWebApiOptions"/>,
        /// each for one downstream web API. You can pass-in null, but in that case <paramref name="downstreamWebApiOptionsOverride"/>
        /// needs to be set.</param>
        /// <param name="inputData">Input data.</param>
        /// <param name="downstreamWebApiOptionsOverride">Overrides the options proposed in the configuration described
        /// by <paramref name="serviceName"/>.</param>
        /// <param name="user">[Optional] Claims representing a user. This is useful in platforms like Blazor
        /// or Azure Signal R, where the HttpContext is not available. In other platforms, the library
        /// will find the user from the HttpContext.</param>
        /// <returns>The value returned by the downstream web API.</returns>
        public static async Task GetForUserAsync <TInput>(
            this IDownstreamWebApi downstreamWebApi,
            string serviceName,
            TInput inputData,
            Action <DownstreamWebApiOptions>?downstreamWebApiOptionsOverride = null,
            ClaimsPrincipal?user = null)
        {
            if (downstreamWebApi is null)
            {
                throw new ArgumentNullException(nameof(downstreamWebApi));
            }

            using StringContent? input = ConvertFromInput(inputData);

            await downstreamWebApi.CallWebApiForUserAsync(
                serviceName,
                downstreamWebApiOptionsOverride,
                user,
                input).ConfigureAwait(false);
        }
Example #7
0
        /// <summary>
        /// Call a web API endpoint with an HttpGet,
        /// and return strongly typed data.
        /// </summary>
        /// <typeparam name="TOutput">Output type.</typeparam>
        /// <param name="downstreamWebApi">The downstream web API.</param>
        /// <param name="serviceName">Name of the service describing the downstream web API. There can
        /// be several configuration named sections mapped to a <see cref="DownstreamWebApiOptions"/>,
        /// each for one downstream web API. You can pass-in null, but in that case <paramref name="downstreamWebApiOptionsOverride"/>
        /// needs to be set.</param>
        /// <param name="downstreamWebApiOptionsOverride">Overrides the options proposed in the configuration described
        /// by <paramref name="serviceName"/>.</param>
        /// <param name="user">[Optional] Claims representing a user. This is useful in platforms like Blazor
        /// or Azure Signal R, where the HttpContext is not available. In other platforms, the library
        /// will find the user from the HttpContext.</param>
        /// <returns>The value returned by the downstream web API.</returns>
        public static async Task <TOutput?> CallWebApiForUserAsync <TOutput>(
            this IDownstreamWebApi downstreamWebApi,
            string serviceName,
            Action <DownstreamWebApiOptions>?downstreamWebApiOptionsOverride = null,
            ClaimsPrincipal?user = null)
            where TOutput : class
        {
            if (downstreamWebApi is null)
            {
                throw new ArgumentNullException(nameof(downstreamWebApi));
            }

            HttpResponseMessage response = await downstreamWebApi.CallWebApiForUserAsync(
                serviceName,
                downstreamWebApiOptionsOverride,
                user,
                null).ConfigureAwait(false);

            return(await ConvertToOutput <TOutput>(response).ConfigureAwait(false));
        }
        /// <summary>
        /// Calls the web API endpoint with an HttpPut, providing strongly typed input data.
        /// </summary>
        /// <typeparam name="TInput">Input type.</typeparam>
        /// <param name="downstreamWebApi">The downstream web API.</param>
        /// <param name="serviceName">Name of the service describing the downstream web API. There can
        /// be several configuration named sections mapped to a <see cref="DownstreamWebApiOptions"/>,
        /// each for one downstream web API. You can pass-in null, but in that case <paramref name="downstreamWebApiOptionsOverride"/>
        /// needs to be set.</param>
        /// <param name="relativePath">Path to the API endpoint relative to the base URL specified in the configuration.</param>
        /// <param name="inputData">Input data sent to the API.</param>
        /// <param name="downstreamWebApiOptionsOverride">Overrides the options proposed in the configuration described
        /// by <paramref name="serviceName"/>.</param>
        /// <param name="user">[Optional] Claims representing a user. This is useful in platforms like Blazor
        /// or Azure Signal R, where the HttpContext is not available. In other platforms, the library
        /// will find the user from the HttpContext.</param>
        /// <param name="authenticationScheme">Authentication scheme. If null, will use OpenIdConnectDefault.AuthenticationScheme
        /// if called from a web app, and JwtBearerDefault.AuthenticationScheme if called from a web API.</param>
        /// <returns>The value returned by the downstream web API.</returns>
        public static async Task PutForUserAsync <TInput>(
            this IDownstreamWebApi downstreamWebApi,
            string serviceName,
            string relativePath,
            TInput inputData,
            Action <DownstreamWebApiOptions>?downstreamWebApiOptionsOverride = null,
            ClaimsPrincipal?user        = null,
            string?authenticationScheme = null)
        {
            if (downstreamWebApi is null)
            {
                throw new ArgumentNullException(nameof(downstreamWebApi));
            }

            using StringContent? input = ConvertFromInput(inputData);

            await downstreamWebApi.CallWebApiForUserAsync(
                serviceName,
                authenticationScheme,
                PrepareOptions(relativePath, downstreamWebApiOptionsOverride, HttpMethod.Put),
                user,
                input).ConfigureAwait(false);
        }
Example #9
0
 public Orders(IDownstreamWebApi integrationWebApi)
 {
     _integrationWebApi = integrationWebApi;
 }
Example #10
0
 public TodoListController(IDownstreamWebApi downstreamWebApi)
 {
     _downstreamWebApi = downstreamWebApi;
 }
 public GreeterService(ILogger <GreeterService> logger,
                       IDownstreamWebApi downstreamWebApi)
 {
     _logger           = logger;
     _downstreamWebApi = downstreamWebApi;
 }
 public DownStreamController(IDownstreamWebApi downstreamWebApi)
 {
     _downstreamWebApi = downstreamWebApi;
 }
 public WeatherForecastController(ILogger <WeatherForecastController> logger,
                                  IDownstreamWebApi downstreamWebApi)
 {
     _logger           = logger;
     _downstreamWebApi = downstreamWebApi;
 }
 public IndexModel(ILogger <IndexModel> logger,
                   IDownstreamWebApi downstreamWebApi)
 {
     _logger           = logger;
     _downstreamWebApi = downstreamWebApi;
 }
Example #15
0
 public TodoListController(IDownstreamWebApi downstreamWebApi, ITokenAcquisition tokenAcquisition)
 {
     _downstreamWebApi = downstreamWebApi;
     _tokenAcquisition = tokenAcquisition;
 }
Example #16
0
 public BackEndApiWithOnBehalfOfFlow(IDownstreamWebApi downstreamWebApi)
 {
     _downstreamWebApi = downstreamWebApi;
 }
Example #17
0
 public IndexModel(ILogger <IndexModel> logger, GraphServiceClient graphServiceClient, IDownstreamWebApi downstreamWebApi)
 {
     _logger             = logger;
     _graphServiceClient = graphServiceClient;
     _downstreamWebApi   = downstreamWebApi;
 }
 public OrdersViaOnBehalfOfFlow(IDownstreamWebApi integrationWebApi)
 {
     _integrationWebApi = integrationWebApi;
 }
Example #19
0
 public HomeController(ILogger <HomeController> logger,
                       IDownstreamWebApi downstreamWebApi)
 {
     _logger           = logger;
     _downstreamWebApi = downstreamWebApi;
 }
Example #20
0
 public CallWebApiController(ILogger <CallWebApiController> logger,
                             IDownstreamWebApi downstreamWebApi)
 {
     _logger           = logger;
     _downstreamWebApi = downstreamWebApi;
 }
Example #21
0
 public SampleFunc(ILogger <SampleFunc> logger,
                   IDownstreamWebApi downstreamWebApi)
 {
     _downstreamWebApi = downstreamWebApi;
     _logger           = logger;
 }