Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddHttpContextAccessor();

            services.AddAntiforgery(options =>
            {
                var antiForgerySection = Configuration.GetSection("Security")
                                         .GetSection("AntiForgery");

                options.HeaderName    = antiForgerySection.GetValue <string>("Header");
                options.FormFieldName = antiForgerySection.GetValue <string>("FormField");

                options.Cookie.HttpOnly = true;
                options.Cookie.Name     = antiForgerySection.GetValue <string>("Cookie");
                options.Cookie.SameSite = SameSiteMode.Strict;

                options.SuppressXFrameOptionsHeader = true;
            });

            var dbSection = Configuration.GetSection("Database");
            var dbConfig  = new DbConfigModel
            {
                Enabled          = dbSection.GetValue <bool>("Enabled"),
                Type             = Enum.Parse <DbmsType>(dbSection.GetValue <string>("Dbms")),
                ConnectionString = dbSection.GetValue <string>("ConnectionString")
            };

            if (dbConfig.Enabled)
            {
                services.AddDatabaseContext(dbConfig);
            }
        }
Exemplo n.º 2
0
        public static DbOption SelectedDbOption()
        {
            DbConfigModel dbConfigModel = GetDbConfigModel();

            DbOption dbOption = dbConfigModel.SelectedDbOption();

            return(dbOption);
        }
Exemplo n.º 3
0
        static DatabaseConfig()
        {
            if (!Directory.Exists(ConfigFolder))
            {
                Directory.CreateDirectory(ConfigFolder);
            }

            if (!File.Exists(ConfigFolder + "/" + ConfigFile))
            {
                Data = new DbConfigModel();
                var json = JsonConvert.SerializeObject(Data, Formatting.Indented);
                File.WriteAllText(ConfigFolder + "/" + ConfigFile, json);
            }
            else
            {
                var json = File.ReadAllText(ConfigFolder + "/" + ConfigFile);
                Data = JsonConvert.DeserializeObject <DbConfigModel>(json);
            }
        }
Exemplo n.º 4
0
        public static IServiceCollection AddDatabaseContext(this IServiceCollection collection, DbConfigModel config)
        {
            Type targetType;

            switch (config.Type)
            {
            case DbmsType.SQLite:
                targetType = typeof(SqliteDatabaseContext);
                break;

            case DbmsType.SQLServer:
                targetType = typeof(SqlServerDatabaseContext);
                break;

            case DbmsType.PostgreSQL:
                targetType = typeof(PostgreSqlDatabaseContext);
                break;

            case DbmsType.MySql:
                targetType = typeof(MysqlDatabaseContext);
                break;

            default:
                throw new NotSupportedException();
            }

            DatabaseContext.Context = Activator.CreateInstance(targetType, config.ConnectionString) as DatabaseContext;
            return(collection);
        }