Exemple #1
0
        /// <summary>
        /// Configure services method
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <AppSettings>(Configuration);
            _settings = Configuration.Get <AppSettings>();


            if (_settings.UseEnviromentalVariables)
            {
                _secrets.MapAppSecretsToEnvVariables();
            }
            else
            {
                services.Configure <AppSecrets>(Configuration);
                _secrets = Configuration.Get <AppSecrets>();
            }

            if (_settings.UseInMemDB)
            {
                services.AddDbContext <PlinxHubContext>(options =>
                                                        options.UseInMemoryDatabase(databaseName: "Identity"));

                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseInMemoryDatabase(databaseName: "PlinxHubDB"));
            }
            else
            {
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(
                                                                 Configuration.GetConnectionString("DefaultConnection")));

                services.AddDbContext <PlinxHubContext>(options =>
                                                        options.UseSqlServer(
                                                            Configuration.GetConnectionString("DefaultConnection")));
            }

            services.AddDefaultIdentity <IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true).AddRoles <IdentityRole>()
            .AddEntityFrameworkStores <PlinxHubContext>();


            services.AddControllersWithViews();

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential
                // cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                // requires using Microsoft.AspNetCore.Http;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddRazorPages();

            if (_settings.EnableSocialLogins)
            {
                services.AddAuthentication().AddGoogle(googleOptions => {
                    googleOptions.ClientId     = _secrets.Authentication.Google.AppID;
                    googleOptions.ClientSecret = _secrets.Authentication.Google.AppSecret;
                })
                .AddFacebook(facebookOptions =>
                {
                    facebookOptions.AppId     = _secrets.Authentication.Facebook.AppID;
                    facebookOptions.AppSecret = _secrets.Authentication.Facebook.AppSecret;
                }).AddTwitter(o => {
                    o.ConsumerKey    = _secrets.Authentication.Twitter.AppID;
                    o.ConsumerSecret = _secrets.Authentication.Twitter.AppSecret;
                }).AddLinkedIn(o => {
                    o.ClientId     = _secrets.Authentication.LinkedIn.AppID;
                    o.ClientSecret = _secrets.Authentication.LinkedIn.AppID;
                });
            }

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

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            // requires
            // using Microsoft.AspNetCore.Identity.UI.Services;
            // using WebPWrecover.Services;
            services.AddScoped <IEmailSender, EmailSender>();

            services.AddScoped <IEmailRepository, EmailRepository>();
            services.AddScoped <IEmailService, EmailService>();
            services.AddScoped <IOrderRepository, OrderRepository>();
            services.AddScoped <IOrderService, OrderService>();
            services.AddScoped <IApiKeyGen, ApiKeyGen>();
            MapperConfiguration config = MapperConfig.Get();

            services.AddSingleton <IMapper>(new Mapper(config));
        }