Пример #1
0
 public static void Configure(IServiceCollection services, IConfiguration configuration)
 {
     ContextInstaller.Configure(services, configuration);
     RepositoryInstallers.Configure(services);
     ServiceInstallers.Configure(services);
 }
Пример #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            // Configure strongly typed settings
            services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));

            var appSettingsSection = Configuration.GetSection("AppSettings");

            services.Configure <AppSettings>(appSettingsSection);

            // Configure Json Web Token Authentication
            var appSettings = appSettingsSection.Get <AppSettings>();
            var key         = Encoding.ASCII.GetBytes(appSettings.JwtSecret);

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });

            // Configure Dependency Injection
            InfrastructureInstallers.Install(services);
            RepositoryInstallers.Install(services);
            ServiceInstallers.Install(services);

            // Configure Swagger
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info {
                    Title = "Sample Web Api", Version = "v1"
                });
                options.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = "header",
                    Type        = "apiKey"
                });
                options.AddSecurityRequirement(
                    new Dictionary <string, IEnumerable <string> >
                {
                    {
                        "Bearer",
                        new string[] { }
                    },
                });
            });
        }