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)
 {
     ConfigureService.ConfigureDependenciesService(services);
     ConfigureRepository.ConfigureDepenciesRepository(services);
     services.AddControllersWithViews();
     // In production, the Angular files will be served from this directory
     services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });
 }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureService.ConfigureDependenciesService(services);
            ConfigureRepository.ConfigureDepenciesRepository(services);

            var config = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new DtoToModelProfile());
                cfg.AddProfile(new EntityToDtoProfile());
                cfg.AddProfile(new ModelToEntityProfile());
            });

            IMapper mapper = config.CreateMapper();

            services.AddSingleton(mapper);

            services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo
            {
                Title       = "API de produtos - Teste DTI",
                Version     = "v1",
                Description = "API criada para teste prático sênior da empresa DTI",
                Contact     = new OpenApiContact
                {
                    Name  = "Rayne Gomes",
                    Email = string.Empty,
                    Url   = new Uri("https://github.com/raynegomes")
                }
            }
                                                     ));

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                {
                    builder.WithOrigins("http://localhost:3000").AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
                });
            });
            //services.AddControllers();
            services.AddControllers()
            .ConfigureApiBehaviorOptions(options =>
            {
                options.InvalidModelStateResponseFactory = context =>
                {
                    var result = new BadRequestObjectResult(context.ModelState);

                    // TODO: add `using using System.Net.Mime;` to resolve MediaTypeNames
                    result.ContentTypes.Add(MediaTypeNames.Application.Json);
                    result.ContentTypes.Add(MediaTypeNames.Application.Xml);

                    return(result);
                };
            });
        }
Exemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigureService.ConfigureDependenciesService(services);
            ConfigureRepository.ConfigureDepenciesRepository(services);
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "My API", Version = "v1"
                });
            });
        }
Exemplo n.º 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            if (_environment.IsEnvironment("Testing"))
            {
                Environment.SetEnvironmentVariable("DB_CONNECTION", "Data Source=LAPTOP-OHKVM7TI;Initial Catalog=dbApi_Integration;Persist Security Info=True;User ID=sa;Password=1234");
                Environment.SetEnvironmentVariable("DATABASE", "SQLSERVER");
                Environment.SetEnvironmentVariable("MIGRATION", "APLICAR");
                Environment.SetEnvironmentVariable("Audience", "ExemploAudience");
                Environment.SetEnvironmentVariable("Issuer", "ExemploIssuer");
                Environment.SetEnvironmentVariable("Seconds", "3600");
            }

            services.AddControllers();

            ConfigureService.ConfigureDepenciesService(services);
            ConfigureRepository.ConfigureDepenciesRepository(services);

            #region AutoMapper

            var config = new AutoMapper.MapperConfiguration(cfg => {
                cfg.AddProfile(new DtoToModelProfile());
                cfg.AddProfile(new EntityToDtoProfile());
                cfg.AddProfile(new ModelToEntityProfile());
            });

            IMapper mapper = config.CreateMapper();

            services.AddSingleton(mapper);

            #endregion

            #region Auth token

            var signinConfigurations = new SigningConfigurations();

            services.AddSingleton(signinConfigurations);

            var tokenConfigurations = new TokenConfigurations();
            //new ConfigureFromConfigurationOptions<TokenConfigurations>(
            //    Configuration
            //    .GetSection("TokenConfigurations"))
            //    .Configure(tokenConfigurations);

            tokenConfigurations.Audience = Environment.GetEnvironmentVariable("Audience");
            tokenConfigurations.Issuer   = Environment.GetEnvironmentVariable("Issuer");
            tokenConfigurations.Seconds  = Convert.ToInt32(Environment.GetEnvironmentVariable("Seconds"));

            services.AddSingleton(tokenConfigurations);

            services.AddAuthentication(authOptions =>
            {
                authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                authOptions.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(bearerOptions => {
                var paramsValidation = bearerOptions.TokenValidationParameters;
                paramsValidation.IssuerSigningKey = signinConfigurations.Key;
                //paramsValidation.ValidAudience = tokenConfigurations.Audience;
                //paramsValidation.ValidIssuer = tokenConfigurations.Issuer;
                paramsValidation.ValidAudience            = Environment.GetEnvironmentVariable("Audience");
                paramsValidation.ValidIssuer              = Environment.GetEnvironmentVariable("Issuer");
                paramsValidation.ValidateIssuerSigningKey = true;
                paramsValidation.ValidateLifetime         = true;
                paramsValidation.ClockSkew = TimeSpan.Zero;
            });

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

            #endregion

            #region Swagger

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Curso de API com ASPNetCore 3.1",
                    Description = "Arquitetura DDD",
                    Contact     = new Microsoft.OpenApi.Models.OpenApiContact
                    {
                        Name  = "Luiz Fernando Bill dos Santos",
                        Email = "*****@*****.**"
                    },
                });

                c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
                {
                    Description = "Entre com o Token JWT",
                    Name        = "Authorization",
                    In          = Microsoft.OpenApi.Models.ParameterLocation.Header,
                    Type        = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey
                });

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

                //c.ResolveConflictingActions(a => a.First());
            });

            #endregion
        }