Пример #1
0
 public YHttpRequestHandler(IYAuthProvider authProvider, IHttpClientFactory httpClientFactory,
                            IOptions <YGraphOptions> graphOptions,
                            IOptions <YHostOptions> apiOptions,
                            IOptions <YMicrosoftIdentityOptions> azureAdOptions)
 {
     this.authProvider      = authProvider;
     this.httpClientFactory = httpClientFactory;
     this.graphOptions      = graphOptions.Value;
     this.apiOptions        = apiOptions.Value;
     this.azureAdOptions    = azureAdOptions.Value;
 }
Пример #2
0
        /// <summary>
        /// Add Swagger generation, with OAuth2 implicit flow configured with Azure Ad
        /// </summary>
        public static IServiceCollection AddAzureOauth2Swagger(
            this IServiceCollection services,
            IConfiguration configuration,
            string azureOptionsSectionName = "AzureAD",
            string configSectionName       = "YgdraServices")
        {
            var options = new YHostOptions();

            configuration.Bind(configSectionName, options);

            var azureOptions = new YMicrosoftIdentityOptions();

            configuration.Bind(azureOptionsSectionName, azureOptions);


            var scopes = options.GetScopes().Select(scope => $"https://{azureOptions.Domain}/{azureOptions.ClientId}/{scope}");


            // Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("OAuth2", new OpenApiSecurityScheme
                {
                    Type  = SecuritySchemeType.OAuth2,
                    Flows = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/authorize"),
                            TokenUrl         = new Uri("https://login.microsoftonline.com/common/oauth2/v2.0/token"),
                            Scopes           = scopes.ToDictionary(s => s, s => s)
                        }
                    }
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme, Id = "OAuth2"
                            },
                        },
                        scopes.ToList()
                    }
                });
            });

            return(services);
        }