示例#1
0
        private Startup()
        {
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json");

            if (Environment.GetEnvironmentVariable("Environment") == "Development")
            {
                configurationBuilder.AddUserSecrets <Startup>();
            }
            else
            {
                Logger.LogInformation($"Start loading encyption keys");
                Configuration = configurationBuilder.Build();
                string address = Environment.GetEnvironmentVariable("KeySource") ?? Configuration["KeySource"];
                configurationBuilder.AddInMemoryCollection(new KeyLoader(address, new AesRsaProtector()).GetKeys());
                Logger.LogInformation($"Encyption keys loaded");
            }
            Configuration = configurationBuilder.Build();

            IServiceCollection services = new ServiceCollection();
            Storage            storage  = Enum.Parse <Storage>(Environment.GetEnvironmentVariable("Storage") ?? Configuration["Storage"]);

            Logger.LogInformation($"Storage type: {storage}");

            services.AddTransient <IProtector, AesRsaProtector>();
            Logger.LogInformation($"Protector type: Aes+Rsa");

            ProviderLogger.CreateInstance(Configuration["logKey"], Configuration["logVector"]);
            Logger.LogInformation($"Provider log prottection enabled");

            switch (storage)
            {
            case Storage.Files:
                services.AddSingleton <ICaptcha, SimpleCaptcha>();
                Logger.LogInformation("Using captcha from prepeared files");

                services.AddSingleton <IAuthorization, UserFileStorage>();
                services.AddSingleton <IStorageFactory, FileStorageFactory>();
                Logger.LogWarning("Password hashing disabled in this configuration");
                break;

            case Storage.InternalDB:
                services.AddDbContext <StorageContext>(options =>
                                                       options.UseSqlite(GetInternalConnectionString()));
                AddDBConfigurationComponents(services);
                break;

            case Storage.ExternalDB:
                services.AddDbContext <StorageContext>(options =>
                                                       options.UseMySql(GetExternalConnectionString()));
                AddDBConfigurationComponents(services);
                break;
            }

            Services = services.BuildServiceProvider();
        }