Beispiel #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddDbContext<MySqlContext>(p => p.UseMySql(Configuration.GetConnectionString("MySqlConnectionString"),
                builder => builder.MigrationsAssembly("API")));

            var signingConfigurations = new SigningConfigurations();
            services.AddSingleton(signingConfigurations);

            var tokenConfigurations = new TokenConfiguration();

            new ConfigureFromConfigurationOptions<TokenConfiguration>(
                Configuration.GetSection("TokenConfigurations")
            )
            .Configure(tokenConfigurations);

            services.AddSingleton(tokenConfigurations);


            services.AddAuthentication(authOptions =>
            {
                authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(bearerOptions =>
            {
                var paramsValidation = bearerOptions.TokenValidationParameters;
                paramsValidation.IssuerSigningKey = signingConfigurations.Key;
                paramsValidation.ValidAudience = tokenConfigurations.Audience;
                paramsValidation.ValidIssuer = tokenConfigurations.Issuer;

                paramsValidation.ValidateIssuerSigningKey = true;

                paramsValidation.ValidateLifetime = true;

                paramsValidation.ClockSkew = TimeSpan.Zero;
            });

            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                    .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                    .RequireAuthenticatedUser().Build());
            });

            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo { 
                    Title = "Api OS", 
                    Version = "v1" 
                });
            });

            services.AddScoped<PessoaService>();
            services.AddScoped<PessoaRepository>();

            services.AddScoped<LoginService>();
            services.AddScoped<LoginRepository>();

            services.AddScoped<OrdemService>();
            services.AddScoped<OrdemRepository>();

            services.AddScoped<FileService>();

            services.AddScoped<AuthorizeAttribute>();

            services.AddControllers();
        }