예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(o => o.AddPolicy("EmailPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials();
            }));

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

            services.Configure <EmailConfiguration>(Configuration.GetSection("EmailConfiguration"));

            // string executableLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
            // string path = Path.GetDirectoryName(executableLocation).Split("bin")[0];
            //string cs = Configuration.GetConnectionString("DefaultConnection");
            // string[] csSplit = cs.Split("=");
            // cs = csSplit[0] + "=" + path + csSplit[1];

            //Choosing a db service
            CheckDB check = new CheckDB();

            //check for environment variables (described in docs/dbconfig.md)
            //if variable is not set, grab from appsettings.json
            String ConnectionString = check.getConnectionStringEnvVar() ?? Configuration.GetConnectionString("DefaultConnection");

            //if not set just use sqlite
            String DatabaseType = check.checkType() ?? "sqlite";

            switch (DatabaseType)
            {
            case "mssql":
                var host     = Configuration["DBHOST"] ?? "172.19.0.1";
                var db       = Configuration["DBNAME"] ?? "openvoting";
                var port     = Configuration["DBPORT"] ?? "1433";
                var username = Configuration["DBUSERNAME"] ?? "sa";
                var password = Configuration["DBPASSWORD"] ?? "Sql!Expre55";

                string connStr = $"Data Source={host},{port};Integrated Security=False;";
                connStr += $"User ID={username};Password={password};Database={db};";
                connStr += $"Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(connStr));
                break;

            case "mysql":
                host     = Configuration["DBHOST"] ?? "localhost";
                port     = Configuration["DBPORT"] ?? "3306";
                password = Configuration["DBPASSWORD"] ?? "secret";
                db       = Configuration["DBNAME"] ?? "openvoting";
                services.AddDbContext <ApplicationDbContext>(options =>
                {
                    options.UseMySql($"server={host}; userid=root; pwd={password};"
                                     + $"port={port}; database={db}");
                });
                break;

            default:     //sqlite
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlite(ConnectionString));
                break;
            }



            /*
             * services.AddDefaultIdentity<IdentityUser>()
             * .AddDefaultUI(UIFramework.Bootstrap4)
             * .AddEntityFrameworkStores<ApplicationDbContext>();*/
            services.AddIdentity <IdentityUser, IdentityRole>(
                option =>
            {
                option.Password.RequireDigit           = false;
                option.Password.RequiredLength         = 6;
                option.Password.RequireNonAlphanumeric = false;
                option.Password.RequireUppercase       = false;
                option.Password.RequireLowercase       = false;
            }
                ).AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders()
            .AddDefaultUI(UIFramework.Bootstrap4);


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title       = "VotingTool API",
                    Version     = "v1",
                    Description = "VotingTool Backend REST API Service Documentation\nhttps://github.com/CstHub/votingtool"
                });
            });

            services.AddLocalization(opts =>
            {
                opts.ResourcesPath = "Resources";
            });

            services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.Configure <RequestLocalizationOptions>(opts =>
            {
                var supportedCultures = new List <CultureInfo> {
                    new CultureInfo("en"),
                    new CultureInfo("fr"),
                };

                opts.DefaultRequestCulture = new RequestCulture("en");
                opts.SupportedCultures     = supportedCultures;
                opts.SupportedUICultures   = supportedCultures;
            });
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(o => o.AddPolicy("EmailPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials();
            }));

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

            services.Configure <EmailConfiguration>(Configuration.GetSection("EmailConfiguration"));
            services.Configure <MapConfiguration>(Configuration.GetSection("MapConfiguration"));

            //Choosing a db service
            CheckDB check = new CheckDB();

            //check for environment variables (described in docs/dbconfig.md)
            //if variable is not set, grab from appsettings.json
            String ConnectionString = check.getConnectionStringEnvVar() ?? Configuration.GetConnectionString("DefaultConnection");

            //if not set just use sqlite
            String DatabaseType = check.checkType() ?? "sqlite";

            switch (DatabaseType)
            {
            case "mssql":
                var host     = Configuration["DBHOST"] ?? "172.19.0.1";
                var db       = Configuration["DBNAME"] ?? "openvoting";
                var port     = Configuration["DBPORT"] ?? "1433";
                var username = Configuration["DBUSERNAME"] ?? "sa";
                var password = Configuration["DBPASSWORD"] ?? "Sql!Expre55";

                string connStr = $"Data Source={host},{port};Integrated Security=False;";
                connStr += $"User ID={username};Password={password};Database={db};";
                connStr += $"Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(connStr));
                break;

            case "mysql":
                host     = Configuration["DBHOST"] ?? "localhost";
                port     = Configuration["DBPORT"] ?? "3306";
                password = Configuration["DBPASSWORD"] ?? "secret";
                db       = Configuration["DBNAME"] ?? "openvoting";
                services.AddDbContext <ApplicationDbContext>(options =>
                {
                    options.UseMySql($"server={host}; userid=root; pwd={password};"
                                     + $"port={port}; database={db}");
                });
                break;

            default:     //sqlite
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlite(ConnectionString));
                break;
            }

            services.AddIdentity <IdentityUser, IdentityRole>(
                option =>
            {
                option.Password.RequireDigit           = false;
                option.Password.RequiredLength         = 6;
                option.Password.RequireNonAlphanumeric = false;
                option.Password.RequireUppercase       = false;
                option.Password.RequireLowercase       = false;
            }
                ).AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders()
            .AddDefaultUI(UIFramework.Bootstrap4);


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Title       = "VotingTool API",
                    Version     = "v1",
                    Description = "VotingTool Backend REST API Service Documentation\nhttps://github.com/CstHub/votingtool"
                });
            });

            services.AddLocalization(opts =>
            {
                opts.ResourcesPath = "Resources";
            });

            services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization(
                // config to use sharedResource as localization provider for data annotation
                options => { options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource)); })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(
                options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            });

            services.AddHttpClient();

            services.Configure <RequestLocalizationOptions>(opts =>
            {
                var supportedCultures = new List <CultureInfo> {
                    new CultureInfo("en"),
                    new CultureInfo("fr"),
                    new CultureInfo("es")
                };

                opts.DefaultRequestCulture = new RequestCulture("en");
                opts.SupportedCultures     = supportedCultures;
                opts.SupportedUICultures   = supportedCultures;
            });

            try
            {
                var local_access_token = Configuration["mapkey"];
                if (!string.IsNullOrEmpty(local_access_token))
                {
                    if (string.IsNullOrEmpty(MapController.AccessToken))
                    {
                        MapController.AccessToken = local_access_token;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
예제 #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.Configure <EmailConfiguration>(Configuration.GetSection("EmailConfiguration"));
            services.Configure <MapConfiguration>(Configuration.GetSection("MapConfiguration"));

            //Choosing a db service
            CheckDB check = new CheckDB();

            //check for environment variables (described in docs/dbconfig.md)
            //if variable is not set, grab from appsettings.json
            String ConnectionString = check.getConnectionStringEnvVar() ?? Configuration.GetConnectionString("DefaultConnection");

            //if not set just use sqlite
            String DatabaseType = check.checkType() ?? "sqlite";

            switch (DatabaseType)
            {
            case "mssql":
                var host     = Configuration["DBHOST"] ?? "172.19.0.1";
                var db       = Configuration["DBNAME"] ?? "openvoting";
                var port     = Configuration["DBPORT"] ?? "1433";
                var username = Configuration["DBUSERNAME"] ?? "sa";
                var password = Configuration["DBPASSWORD"] ?? "Sql!Expre55";

                string connStr = $"Data Source={host},{port};Integrated Security=False;";
                connStr += $"User ID={username};Password={password};Database={db};";
                connStr += $"Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlServer(connStr));
                break;

            case "mysql":
                host     = Configuration["DBHOST"] ?? "localhost";
                port     = Configuration["DBPORT"] ?? "3306";
                password = Configuration["DBPASSWORD"] ?? "secret";
                db       = Configuration["DBNAME"] ?? "openvoting";
                services.AddDbContext <ApplicationDbContext>(options =>
                {
                    options.UseMySql($"server={host}; userid=root; pwd={password};"
                                     + $"port={port}; database={db}");
                });
                break;

            default:     //sqlite
                services.AddDbContext <ApplicationDbContext>(options =>
                                                             options.UseSqlite(ConnectionString));
                break;
            }

            // Add Cors
            services.AddCors(o => o.AddPolicy("PlanVotePolicy", builder => {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            services.AddDefaultIdentity <IdentityUser>(
                options =>
            {
                options.SignIn.RequireConfirmedAccount = true;
            }
                ).AddEntityFrameworkStores <ApplicationDbContext>();

            // services.AddIdentity<IdentityUser, IdentityRole>(
            //     option =>
            //     {
            //         option.Password.RequireDigit = false;
            //         option.Password.RequiredLength = 6;
            //         option.Password.RequireNonAlphanumeric = false;
            //         option.Password.RequireUppercase = false;
            //         option.Password.RequireLowercase = false;
            //     }
            // ).AddEntityFrameworkStores<ApplicationDbContext>()
            // .AddDefaultTokenProviders();

            services.AddLocalization(opts =>
            {
                opts.ResourcesPath = "Resources";
            });

            services.AddControllersWithViews();
            services.AddRazorPages();
        }