Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var appsettings = Configuration.GetSection("Appsettings").Get <Appsettings>();

            services.AddControllers();
            // Add Cors
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder
                                  .WithOrigins(appsettings.CorsPolicy) // the Angular app url
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            });
            // Add Db
            services.AddDbContext <DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddOptions();

            services.Configure <MailSetting_Dto>(Configuration.GetSection("MailSettings"));

            //Json
            services.AddControllersWithViews()
            .AddNewtonsoftJson(options =>
                               options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                               );

            // JwtBear
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII
                                                                        .GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
                    ValidateIssuer   = false,
                    ValidateAudience = false
                };
            });

            // Auto Mapper
            services.AddAutoMapper(typeof(Startup));
            services.AddScoped <IMapper>(sp =>
            {
                return(new Mapper(AutoMapperConfig.RegisterMappings()));
            });
            services.AddSingleton(AutoMapperConfig.RegisterMappings());

            // Repositories
            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <IRolesRepository, RolesRepository>();
            services.AddScoped <IRoleUserRepository, RoleUserRepository>();
            services.AddScoped <IArticleCategoryRepository, ArticleCategoryRepository>();
            services.AddScoped <IArticleRepository, ArticleRepository>();
            services.AddScoped <IProductCategoryRepository, ProductCategoryRepository>();
            services.AddScoped <IProductRepository, ProductRepository>();
            services.AddScoped <IUploadFileRepository, UploadFileRepository>();

            // Services
            services.AddScoped <IAuthService, AuthService>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IArticleCategoryService, ArticleCategoryService>();
            services.AddScoped <IArticleService, ArticleService>();
            services.AddScoped <IProductCategoryService, ProductCategoryService>();
            services.AddScoped <IProductService, ProductService>();
            services.AddScoped <IDropzoneService, DropzoneService>();

            services.AddScoped <ISendMailService, SendMailService>();
            services.AddScoped <IMailUtility, MailUtility>();
            services.AddScoped <ISendMailByGmail, SendMailByGmail>();

            // Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "API", Version = "v1"
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    In          = ParameterLocation.Header,
                    Description = "Please insert JWT with Bearer into field",
                    Name        = "Authorization",
                    Type        = SecuritySchemeType.ApiKey
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
            });

            services.AddSignalR();
        }