Пример #1
0
        /// <summary>
        /// Add support to call Microsoft Graph. From a named options and a configuration method.
        /// </summary>
        /// <param name="builder">Builder.</param>
        /// <param name="configureMicrosoftGraphOptions">Method to configure the options.</param>
        /// <returns>The builder to chain.</returns>
        public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
            this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
            Action <MicrosoftGraphOptions> configureMicrosoftGraphOptions)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            // https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
            builder.Services.AddOptions <MicrosoftGraphOptions>().Configure(configureMicrosoftGraphOptions);

            builder.Services.AddScoped <GraphServiceClient, GraphServiceClient>(serviceProvider =>
            {
                var tokenAquisitionService = serviceProvider.GetRequiredService <ITokenAcquisition>();
                var options = serviceProvider.GetRequiredService <IOptions <MicrosoftGraphOptions> >();

                var microsoftGraphOptions = options.Value;
                if (microsoftGraphOptions.Scopes == null)
                {
                    throw new ArgumentException(IDWebErrorMessage.CalledApiScopesAreNull);
                }

                string graphBaseUrl    = microsoftGraphOptions.BaseUrl;
                string[] initialScopes = microsoftGraphOptions.Scopes.Split(' ');

                GraphServiceClient client = string.IsNullOrWhiteSpace(graphBaseUrl) ?
                                            new GraphServiceClient(new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes)) :
                                            new GraphServiceClient(graphBaseUrl, new TokenAcquisitionCredentialProvider(tokenAquisitionService, initialScopes));
                return(client);
            });
            return(builder);
        }
Пример #2
0
 /// <summary>
 /// Add support to call Microsoft Graph. From a named option and a configuration section.
 /// </summary>
 /// <param name="builder">Builder.</param>
 /// <param name="configurationSection">Configuration section.</param>
 /// <returns>The builder to chain.</returns>
 public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
     this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
     IConfigurationSection configurationSection)
 {
     return(builder.AddMicrosoftGraph(
                options => configurationSection.Bind(options)));
 }
Пример #3
0
 /// <summary>
 /// Add support to call Microsoft Graph. From a base Graph URL and a default scope.
 /// </summary>
 /// <param name="builder">Builder.</param>
 /// <param name="graphBaseUrl">Named instance of option.</param>
 /// <param name="defaultScopes">Configuration section.</param>
 /// <returns>The builder to chain.</returns>
 public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraph(
     this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
     string graphBaseUrl  = Constants.GraphBaseUrlV1,
     string defaultScopes = Constants.UserReadScope)
 {
     return(builder.AddMicrosoftGraph(
                options =>
     {
         options.BaseUrl = graphBaseUrl;
         options.Scopes = defaultScopes;
     }));
 }
        /// <summary>
        /// Adds a named downstream web API service related to a specific configuration section.
        /// </summary>
        /// <param name="builder">Builder.</param>
        /// <param name="serviceName">Name of the configuration for the service.
        /// This is the name used when calling the service from controller/pages.</param>
        /// <param name="configuration">Configuration.</param>
        /// <returns>The builder for chaining.</returns>
        public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddDownstreamWebApi(
            this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
            string serviceName,
            IConfiguration configuration)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Configure <DownstreamWebApiOptions>(serviceName, configuration);
            builder.Services.AddHttpClient <IDownstreamWebApi, DownstreamWebApi>();
            return(builder);
        }
        /// <summary>
        /// Adds a named downstream web API service initialized with delegates.
        /// </summary>
        /// <param name="builder">Builder.</param>
        /// <param name="serviceName">Name of the configuration for the service.
        /// This is the name which will be used when calling the service from controller/pages.</param>
        /// <param name="configureOptions">Action to configure the options.</param>
        /// <returns>The builder for chaining.</returns>
        public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddDownstreamWebApi(
            this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
            string serviceName,
            Action <DownstreamWebApiOptions> configureOptions)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Configure <DownstreamWebApiOptions>(serviceName, configureOptions);

            // https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests
            builder.Services.AddHttpClient <IDownstreamWebApi, DownstreamWebApi>();
            builder.Services.Configure(serviceName, configureOptions);
            return(builder);
        }
        /// <summary>
        /// Add support to call Microsoft Graph.
        /// </summary>
        /// <param name="builder">Builder.</param>
        /// <param name="graphServiceClientFactory">Function to create a GraphServiceClient.</param>
        /// <returns>The builder to chain.</returns>
        public static MicrosoftIdentityAppCallsWebApiAuthenticationBuilder AddMicrosoftGraphAppOnly(
            this MicrosoftIdentityAppCallsWebApiAuthenticationBuilder builder,
            Func <IAuthenticationProvider, GraphServiceClient> graphServiceClientFactory)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddScoped <GraphServiceClient, GraphServiceClient>(serviceProvider =>
            {
                ITokenAcquisition?tokenAquisitionService = serviceProvider.GetRequiredService <ITokenAcquisition>();

                return(graphServiceClientFactory(new TokenAcquisitionAuthenticationProvider(
                                                     tokenAquisitionService,
                                                     new TokenAcquisitionAuthenticationProviderOption()
                {
                    AppOnly = true
                })));
            });
            return(builder);
        }