예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
                            options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddControllers();

            //services.AddDbContext<ContextCompany>(o => o.UseMySql(Configuration.GetConnectionString("Company")));
            services.AddDbContextPool <ContextCompany>(builder =>
            {
                if (_env.IsDevelopment())
                {
                    builder.EnableSensitiveDataLogging(true);
                }

                var connStr = this.Configuration.GetConnectionString("Company");
                builder.UseMySql(connStr);
            });

            InjetorDependency.Registrar(services);

            // Auto Mapper Configurations
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingEntity());
            });

            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);


            services.AddCors(o => o.AddPolicy("Company", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin();
            }));

            //services.AddMatchingInterface(typeof(IUnitOfWork).Assembly);

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                // https://medium.com/@didourebai/add-swagger-to-asp-net-core-3-0-web-api-874cb265854c
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Company", Version = "v1"
                });
            });

            // https://balta.io/blog/aspnetcore-3-autenticacao-autorizacao-bearer-jwt
            //Autenticação e Autorização com Bearer e JWT
            var key = Encoding.ASCII.GetBytes(Settings.Secret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });
            // Adicionando injeção de indenpendencia de configuration para pegar os valores setados no controle
            services.AddSingleton <IConfiguration>(Configuration);
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                //options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
                options.EnableEndpointRouting = false;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            //Add XML format support
            services.AddControllers()
            .AddXmlSerializerFormatters();

            services.AddDbContextPool <Context_ABM_Data_Systems>(builder =>
            {
                if (_env.IsDevelopment())
                {
                    builder.EnableSensitiveDataLogging(true);
                }

                var connStr = this.Configuration.GetConnectionString("ABM_Data_Systems");
                builder.UseMySql(connStr);
            });

            InjetorDependency.Registrar(services);

            // Auto Mapper Configurations
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingEntity());
            });

            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);


            services.AddCors(o => o.AddPolicy("ABM_Data_Systems", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin();
            }));

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "ABM Data Systems", Version = "v1"
                });
            });

            //Autenticação e Autorização com Bearer e JWT
            var key = Encoding.ASCII.GetBytes(Settings.Secret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });

            // Adicionando injeção de indenpendencia de configuration para pegar os valores setados no controle
            services.AddSingleton <IConfiguration>(Configuration);
        }