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)
        {
            JwtSettings jwtSettings = new JwtSettings();

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

            services.AddSingleton <ITokenService, TokenService>(x => new TokenService(jwtSettings.ValidIssuer,
                                                                                      jwtSettings.ValidAudience, jwtSettings.IssuerSigningKey, jwtSettings.TokenLifeTime));

            Binder.BindContext(services);

            services.AddScoped <IUserService, UserService>();

            services.AddScoped <IPetService, PetService>();

            services.AddScoped <IToyService, ToyService>();

            services.AddScoped <IAccessService, AccessService>();

            services.AddScoped <IConnectionService, ConnectionService>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(configureOptions =>
            {
                configureOptions.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidIssuer    = jwtSettings.ValidIssuer,

                    ValidateAudience = true,
                    ValidAudience    = jwtSettings.ValidAudience,

                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Convert.FromBase64String(jwtSettings.IssuerSigningKey)),
                    ValidateLifetime         = false
                };
            });

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new Info()
                {
                    Title = "PetPlay API", Version = "v1"
                });
            });

            services.AddAutoMapper();

            services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.Formatting            = Newtonsoft.Json.Formatting.Indented;
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            });
        }