Example #1
0
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.EnableCors(new MyCorsPolicyProvider());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );



            var container = new Container();

            ModelDI.Confiure(container);

            ServicesDI.Confiure(container);

            container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

            container.Verify();

            GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);
        }
Example #2
0
 public static void AddConfiguration(this IServiceCollection service)
 {
     DBConfiguration.Register(service);
     RepositoriesDI.Register(service);
     ServicesDI.Register(service);
     CorsConfiguration.RegisterCors(service);
     SwaggerConfiguration.RegisterSwagger(service);
 }
        public static void AddConfiguration(this IServiceCollection service, IConfiguration configuration)
        {
            RepositoriesDI.Register(service);
            ServicesDI.Register(service);
            SwaggerConfiguration.RegisterSwagger(service);
            SerilogConfiguration.CreateLogger();

            service.AddBindConfiguration(configuration);
        }
Example #4
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(o => o.AddPolicy(MyPolicyName, builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer    = AuthOptions.ISSUER,

                    ValidateAudience = true,
                    ValidAudience    = AuthOptions.AUDIENCE,
                    ValidateLifetime = true,

                    IssuerSigningKey         = AuthOptions.GetSymmetricSecurityKey(),
                    ValidateIssuerSigningKey = true,
                };
            });

            services.AddControllers()
            .AddNewtonsoftJson(options =>
                               options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                               );

            services.AddEntityFrameworkNpgsql()
            .AddDbContext <ApplicationContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

            // swagger docs, can multiple
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = "API",
                    Version     = "v1",
                    Description = "ASP.NET Core Web API",
                    //TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name  = "Simon Smol",
                        Email = "*****@*****.**",
                        //Url = new Uri("https://example.com/smol"),
                    },
                    //License = new OpenApiLicense
                    //{
                    //    Name = "Use under LICX",
                    //    Url = new Uri("https://example.com/license"),
                    //}
                });

                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name   = "Bearer",
                            In     = ParameterLocation.Header,
                        },
                        new List <string>()
                    }
                });

                // swagger path
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            ServicesDI.Configure(services);
        }