public IServiceProvider GetServiceProvider()
        {
            var services = new ServiceCollection();

            services.AddEntityFrameworkInMemoryDatabase()
            .AddDbContext <ApplicationDbContext>(opt => opt.UseInMemoryDatabase("TestDB"));

            services.AddIdentity <AppIdentityUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpAuthenticationFeature>(new HttpAuthenticationFeature());

            services.AddTransient <IHttpContextAccessor>(h => new HttpContextAccessor {
                HttpContext = httpContext
            });

            var appConfigurator = new AppConfigurator();

            appConfigurator.ConfigureServices(services);

            services.AddSingleton <IConfiguration>(c => new Mock <IConfiguration>().Object);

            var serviceProvider = services.BuildServiceProvider();

            return(serviceProvider);
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <BenchmarkDataContext>(options =>
                                                         options.UseSqlServer(Configuration.GetConnectionString(Configuration["ConnectionToUse"])));
            services.AddMvc();

            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigin",
                                  builder =>
                {
                    builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
                });
            });
            appConfigurator.ConfigureServices(services);
        }
Ejemplo n.º 3
0
        public IServiceProvider GetServiceProvider()
        {
            var services = new ServiceCollection();

            services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(TestConfig.ConnectionString));

            services.AddIdentity <AppIdentityUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>();

            var httpContext = new DefaultHttpContext();

            httpContext.Features.Set <IHttpAuthenticationFeature>(new HttpAuthenticationFeature());
            services.AddSingleton <IHttpContextAccessor>(h => new HttpContextAccessor {
                HttpContext = httpContext
            });

            var appConfigurator = new AppConfigurator();

            appConfigurator.ConfigureServices(services);

            var serviceProvider = services.BuildServiceProvider();

            return(serviceProvider);
        }
Ejemplo n.º 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton <IConfiguration>(_configuration);
            services.AddSingleton <IEmailConfiguration>(_configuration.GetSection("EmailConfiguration").Get <EmailConfiguration>());

            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection")));

            services.AddMvcCore()
            .AddFormatterMappings()
            .AddCacheTagHelper()
            .AddJsonFormatters()
            .AddCors()
            .AddAuthorization(opt =>
            {
            });

            // Setting password requirements
            services.AddIdentity <AppIdentityUser, IdentityRole>(options => {
                options.Password.RequiredLength         = 4;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireDigit           = false;
                options.Password.RequiredUniqueChars    = 0;
                options.Password.RequireLowercase       = false;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            // Configure IdentityServer with in-memory stores, keys, clients and res
            services.AddIdentityServer(options =>
                                       options.PublicOrigin = _configuration.GetSection("ServerHost").Value)
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddAspNetIdentity <AppIdentityUser>()

            /*
             * // this adds the operational data from DB (codes, tokens, consents)
             *
             * .AddOperationalStore(options =>
             * {
             *  options.ConfigureDbContext = builder =>
             *  {
             *      builder.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"),
             *          sql =>
             *          {
             *              sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
             *          });
             *  };
             *
             *  // this enables automatic token cleanup. this is optional.
             *  options.EnableTokenCleanup = true;
             *  options.TokenCleanupInterval = 30; // interval in seconds
             * })*/;

            services.AddAuthentication(options =>
            {
                options.DefaultScheme             = IdentityServerAuthenticationDefaults.AuthenticationScheme;
                options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme;
            })
            .AddIdentityServerAuthentication(options =>
            {
                options.Authority            = _configuration.GetSection("ServerHost").Value;
                options.RequireHttpsMetadata = false;
                options.ApiName   = Constants.ApiName;
                options.ApiSecret = Constants.ClientSecret;
            });

            // Removing cookie authentitication
            services.ConfigureApplicationCookie(options =>
            {
                options.Events.OnRedirectToLogin = context =>
                {
                    context.Response.StatusCode = 401;
                    return(Task.CompletedTask);
                };
            });

            // Add application services.
            _appConfigurator.ConfigureServices(services);

            services.AddMvc();
        }
Ejemplo n.º 5
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var appConfigurator = new AppConfigurator(Configuration);

            return(appConfigurator.ConfigureServices(services));
        }