Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ApplicationSettingsManager <ApplicationSettings> .BindToObject(Configuration);

            JwtSettings jwtSettings = new JwtSettings();

            Configuration.Bind(nameof(JwtSettings), jwtSettings);

            services.AddMemoryCache();
            services.AddDbContext <ApplicationDbContext>();
            services.AddSingleton <DapperDbContext>();
            services.ConfigureBLLServices();
            services.AddAutoMapper();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    // will the lifetime be validated
                    ValidateLifetime = jwtSettings.ValidateLifetime,

                    // specifies whether the publisher will validate when validating the token
                    ValidateIssuer = jwtSettings.ValidateIssuer,

                    // a string representing the publisher
                    ValidIssuer = jwtSettings.ValidIssuer,

                    // setting the token consumer
                    ValidAudience = jwtSettings.ValidAudience,

                    // Will the token consumer be validated
                    ValidateAudience = jwtSettings.ValidateAudience,

                    // validate the security key
                    ValidateIssuerSigningKey = jwtSettings.ValidateIssuerSigningKey,

                    // set the security key
                    IssuerSigningKey =
                        new SymmetricSecurityKey(Convert.FromBase64String(jwtSettings.IssuerSigningKey))
                };
            });

            services.AddAuthorization();

            services.AddCors();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title       = "ArTour API",
                    Description = "Swagger Core API documentation",
                    Version     = "v1"
                });
                var xmlPath = AppDomain.CurrentDomain.BaseDirectory + @"Artour.WebAPI.xml";
                c.IncludeXmlComments(xmlPath);
                c.OperationFilter <FileOperationFilter>();
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }