Exemplo n.º 1
0
 public static string GetConnectionString(
     DBMSSetting dbmsSetting,
     ConnectionString connectionString,
     SQLServerConnectionString sqlServerConnectionString,
     PostgreSQLConnectionString postgreSQLConnectionString)
 {
     if (dbmsSetting.UseMSSQL)
     {
         return(sqlServerConnectionString.UsersConnectionString);
     }
     else if (dbmsSetting.UsePostgreSQL)
     {
         return(postgreSQLConnectionString.UsersConnectionString);
     }
     else
     {
         return(connectionString.DefaultConnectionString);
     }
 }
Exemplo n.º 2
0
        // ConfigureServices is where you register dependencies.
        // This method gets called by the runtime.
        // Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add services to the collection.
            services
            .AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddXmlDataContractSerializerFormatters()
            .AddMvcOptions(options =>
            {
                options.FormatterMappings.SetMediaTypeMappingForFormat(
                    Format.XmlFormat.Xml,
                    new MediaTypeHeaderValue(Format.XmlFormat.ApplicationXml));
            });

            services.AddMemoryCache();

            _jwtSetting = Configuration.GetSection(nameof(JwtSetting))
                          .Get <JwtSetting>();
            _dbmsSetting = Configuration.GetSection(nameof(DBMSSetting))
                           .Get <DBMSSetting>();
            _connectionString = Configuration.GetSection(nameof(ConnectionString))
                                .Get <ConnectionString>();
            _sqlServerConnectionString = Configuration.GetSection(nameof(SQLServerConnectionString))
                                         .Get <SQLServerConnectionString>();
            _postgreSQLConnectionString = Configuration.GetSection(nameof(PostgreSQLConnectionString))
                                          .Get <PostgreSQLConnectionString>();
            _cacheSetting = Configuration.GetSection(nameof(CacheSetting))
                            .Get <CacheSetting>();
            _appSetting = Configuration.GetSection(nameof(AppSetting))
                          .Get <AppSetting>();

            services
            .Configure <JwtSetting>(
                Configuration.GetSection(nameof(JwtSetting)))
            .Configure <DBMSSetting>(
                Configuration.GetSection(nameof(DBMSSetting)))
            .Configure <ConnectionString>(
                Configuration.GetSection(nameof(ConnectionString)))
            .Configure <SQLServerConnectionString>(
                Configuration.GetSection(nameof(SQLServerConnectionString)))
            .Configure <PostgreSQLConnectionString>(
                Configuration.GetSection(nameof(PostgreSQLConnectionString)))
            .Configure <CacheSetting>(
                Configuration.GetSection(nameof(CacheSetting)))
            .Configure <AppSetting>(
                Configuration.GetSection(nameof(AppSetting)));

            // Set authentication scheme for JWT and set JWT provider configuration.
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.SaveToken                 = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = false,
                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(
                        Encoding.ASCII.GetBytes(_jwtSetting.Key))
                };
            });

            // Register the Swagger generator, defining 1 or more Swagger documents.
            services.AddSwaggerGen(swagger =>
            {
                swagger.SwaggerDoc("v1", new Info
                {
                    Title   = Parameters.Authorization,
                    Version = "v1"
                });
                swagger.AddSecurityDefinition(Parameters.Bearer, new ApiKeyScheme
                {
                    In          = Parameters.Header.FirstCharToLower(),
                    Description = Resource.SwaggerSecurityDefinition,
                    Name        = Parameters.Authorization,
                    Type        = Parameters.ApiKey.FirstCharToLower()
                });
                swagger.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> >
                {
                    { Parameters.Bearer, new string[] { } }
                });
            });

            // Create the IServiceProvider based on the container.
            return(AutofacConfigurator.GetAutofacServiceProvider(
                       services,
                       ApplicationContainer,
                       DBMSSetting.GetDBMS(_dbmsSetting),
                       DBMSSetting.GetConnectionString(
                           _dbmsSetting,
                           _connectionString,
                           _sqlServerConnectionString,
                           _postgreSQLConnectionString)
                       ));
        }