// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ConfigureService.ConfigureDependencyService(services); ConfigureRepository.ConfigureDependencyRepository(services); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "Curso AspNetCore 2.2", Version = "v1", Description = "Exemplo de API REST criada com .NET Core", Contact = new Contact { Name = "Lucas Manara Patriarca", Url = "https://github.com/lucasmanara" } }); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
public void ConfigureServices(IServiceCollection services) { if (_environment.IsEnvironment("Testing")) { Environment.SetEnvironmentVariable("DB_CONNECTION", "Persist Security Info=True;Server=localhost;Port=3306;Database=dbAPI;Uid=root;Pwd=******"); Environment.SetEnvironmentVariable("DATABASE", "MYSQL"); Environment.SetEnvironmentVariable("MIGRATION", "APLICAR"); Environment.SetEnvironmentVariable("Audience", "ExemploAudience"); Environment.SetEnvironmentVariable("Issuer", "ExemploIssuer"); Environment.SetEnvironmentVariable("Seconds", "3600"); } ConfigureService.ConfigureDependenciesService(services); ConfigureRepository.ConfigureDependencyRepository(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); var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOptions => { var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfigurations.Key; paramsValidation.ValidAudience = Environment.GetEnvironmentVariable("Audience"); paramsValidation.ValidIssuer = Environment.GetEnvironmentVariable("Audience"); paramsValidation.ValidateIssuerSigningKey = true; paramsValidation.ValidateLifetime = true; paramsValidation.ClockSkew = TimeSpan.Zero; }); services.AddAuthorization(auth => { auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme) .RequireAuthenticatedUser().Build()); }); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Curso de API com AspNetCore 3.1 - Na Prática", Description = "Arquitetura DDD", TermsOfService = new Uri("http://www.mfrinfo.com.br"), Contact = new OpenApiContact { Name = "Marcos Fabricio Rosa", Email = "*****@*****.**", Url = new Uri("http://www.mfrinfo.com.br") }, License = new OpenApiLicense { Name = "Termo de Licença de Uso", Url = new Uri("http://www.mfrinfo.com.br") } } ); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Entre com o Token JWT.", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, new List <string>() } }); }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { ConfigureService.ConfigureDependencyService(services); ConfigureRepository.ConfigureDependencyRepository(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); var signingConfiguration = new SigningConfiguration(); services.AddSingleton(signingConfiguration); var tokenConfiguration = new TokenConfiguration(); new ConfigureFromConfigurationOptions <TokenConfiguration>( Configuration.GetSection("TokenConfiguration")).Configure(tokenConfiguration); services.AddSingleton(tokenConfiguration); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; } ).AddJwtBearer(bearerOptions => { var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfiguration.Key; paramsValidation.ValidAudience = tokenConfiguration.Audience; paramsValidation.ValidIssuer = tokenConfiguration.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.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Projeto API ASP .NET Core 3.1", Version = "v1", Contact = new OpenApiContact { Name = "Guilherme Fiola", Url = new Uri("https://github.com") } }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { In = ParameterLocation.Header, Description = "Entre com o token JWT", Name = "Authorization", Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "oauth2", Name = "Bearer", In = ParameterLocation.Header, }, new List <string>() } }); }); }
// 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", "Server=127.0.0.1; port=5432; user id = postgres; password = postgres; database=aspnetcore_integration; pooling = true"); Environment.SetEnvironmentVariable("DATABASE", "POSTGRES"); Environment.SetEnvironmentVariable("MIGRATION", "APLICAR"); Environment.SetEnvironmentVariable("Audience", "ExemploAudience"); Environment.SetEnvironmentVariable("Issuer", "ExemploIssuer"); Environment.SetEnvironmentVariable("Seconds", "120"); } services.AddControllers(); ConfigureService.ConfigureDependencysService(services); ConfigureRepository.ConfigureDependencyRepository(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); var signingConfigurations = new SigningConfigurations(); services.AddSingleton(signingConfigurations); services.AddAuthentication(authOptions => { authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; authOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(bearerOptions => { var paramsValidation = bearerOptions.TokenValidationParameters; paramsValidation.IssuerSigningKey = signingConfigurations.Key; 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()); }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "Curso de API com AspNetCore 3.1 - Na Prática", Description = "Arquitetura DDD", TermsOfService = new Uri("http://www.google.com"), Contact = new OpenApiContact { Name = "Pablo Henrique Groeler", Email = "*****@*****.**", Url = new Uri("Http://www.google.com") }, License = new OpenApiLicense { Name = "Termo de licença de Uso", Url = new Uri("http://www.google.com.br") } }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Entre com o token JWT", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, new List <string>() } }); }); }