コード例 #1
0
 public static void ConfigureDbContext(this IServiceCollection services, ConnectionStringsOptions connectionStringsOptions)
 {
     services
     .AddDbContext <AdminAppContext>(opts => opts
                                     .UseSqlServer(connectionStringsOptions.DefaultConnection, b =>
     {
         b.EnableRetryOnFailure(3);
         b.MigrationsAssembly("AdminApp.Data");
     })
                                     .UseLoggerFactory(DebugLoggerFactory)
                                     .EnableSensitiveDataLogging())
     //.AddScoped(_ => new AdminAppContext(connectionStringsOptions.DefaultConnection))
     ;
 }
コード例 #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(opt =>
            {
                opt.AddPolicy("AppPolicy", builder =>
                {
                    builder
                    .WithOrigins("http://localhost:3000")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .WithExposedHeaders("Content-Disposition", "x-suggested-filename");
                });
            });

            services.AddTransient <IInvoiceService, InvoiceService>();
            services.AddSingleton <IEmailSender, EmailSender>();

            services.AddAutoMapper(typeof(Startup));

            services.Configure <ConnectionStringsOptions>(Configuration
                                                          .GetSection("ConnectionString"));
            services.Configure <InvoicesOptions>(Configuration
                                                 .GetSection("Invoices"));
            services.Configure <AuthOptions>(Configuration
                                             .GetSection("Auth"));
            services.Configure <SmtpOptions>(Configuration
                                             .GetSection("SmtpOptions"));

            IConfigurationSection connectionSection = Configuration.GetSection("ConnectionStrings");
            var connectionStringOptions             = new ConnectionStringsOptions();

            connectionSection.Bind(connectionStringOptions);

            IConfigurationSection authSection = Configuration.GetSection("Auth");
            var authOptions = new AuthOptions();

            authSection.Bind(authOptions);

            services.AddDbContextPool <InvoiceDbContext>(opt => opt
                                                         .UseSqlServer(connectionStringOptions.DefaultConnection));

            services.AddIdentity <IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores <InvoiceDbContext>()
            .AddDefaultTokenProviders();

            services.AddAuthentication(opt =>
            {
                opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                opt.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(opt =>
            {
                opt.SaveToken            = true;
                opt.RequireHttpsMetadata = false;

                var secretByte = Encoding.UTF8.GetBytes(authOptions.Secret);
                var key        = new SymmetricSecurityKey(secretByte);
                opt.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidIssuer      = authOptions.Issuer,
                    ValidAudience    = authOptions.Audience,
                    IssuerSigningKey = key,
                    ClockSkew        = TimeSpan.Zero
                };
            });

            services.Configure <DataProtectionTokenProviderOptions>(opt =>
            {
                opt.TokenLifespan = TimeSpan.FromHours(1);
            });

            services
            .AddControllers()
            .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining <Startup>())
            .AddNewtonsoftJson();
        }
コード例 #3
0
 public StatusRepository(IOptions <ConnectionStringsOptions> connectionStringsOptions)
 {
     this.connectionStringsOptions = connectionStringsOptions.Value;
 }
コード例 #4
0
 public LocationController(Func <ISpecialService> serviceFunc, IOptions <ConnectionStringsOptions> options)
 {
     this.serviceFunc = serviceFunc;
     this.options     = options.Value;
 }
コード例 #5
0
 public MoviesController(ApplicationDbContext context,
                         IOptions <ConnectionStringsOptions> connectionStringOptions)
 {
     _context = context;
     _connectionStringOptions = connectionStringOptions.Value;
 }
コード例 #6
0
 public FilesServices(IOptions <ConnectionStringsOptions> connectionStringsOptions,
                      ILogger <FilesServices> logger)
 {
     _connectionStringsOptions = connectionStringsOptions.Value;
     _logger = logger;
 }
 public OptionsPatternController(IOptions <EntityFrameworkOptions> entityFrameworkOptions, IOptions <ConnectionStringsOptions> connectionStringsOptions)
 {
     _entityFrameworkOptions   = entityFrameworkOptions.Value;
     _connectionStringsOptions = connectionStringsOptions.Value;
 }
コード例 #8
0
 public WeatherService(IHttpClientFactory clientFactory, IOptions <ConnectionStringsOptions> options)
 {
     this.clientFactory = clientFactory;
     this.options       = options.Value;
 }