Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging();

            // Read which IDP should be used, actively
            var activeIdp = Configuration.GetValue <string>(IDPUSE_CONFIG);

            if (string.IsNullOrWhiteSpace(activeIdp))
            {
                activeIdp = IDSRV_CONFIGSECTION;
            }

            // Adding JWT Bearer Token authentication and validation settings based on appsettings.json
            if (activeIdp.ToLower().Equals(IDSRV_CONFIGSECTION.ToLower()))
            {
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.Authority            = Configuration.GetSection(IDSRV_CONFIGSECTION).GetValue <string>("Url");
                    options.Audience             = Configuration.GetSection(IDSRV_CONFIGSECTION).GetValue <string>("AudienceUri");
                    options.RequireHttpsMetadata = false;
                });
            }
            else if (activeIdp.ToLower().Equals(AAD_CONFIGSECTION.ToLower()))
            {
                services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
                .AddAzureADBearer(options => Configuration.Bind(key: AAD_CONFIGSECTION, instance: options));
                services.Configure <AzureADOptions>(options => Configuration.Bind(key: AAD_CONFIGSECTION, instance: options));
                services.Configure <JwtBearerOptions>(options =>
                {
                    options.Audience = Configuration.GetSection(AAD_CONFIGSECTION).GetValue <string>("AudienceUri");
                });
            }
            else
            {
                throw new System.Exception($"Invalid configuration for element {IDPUSE_CONFIG}. Allowed values are '{AAD_CONFIGSECTION}' or '{IDSRV_CONFIGSECTION}'");
            }

            // Allow access to the HTTP Context from controllers / API implementations
            services.AddHttpContextAccessor();

            // Add the service that implements the business logic
            services.AddTransient <TestService>();

            // Now activate the API controllers
            services.AddControllers();

            // Adding Swashbuckle for Swagger UI Gen
            SwaggerConfig.ConfigureSwaggerServices(services);
        }