예제 #1
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Add CORS support.
            // Must be first to avoid OPTIONS issues when calling from Angular/Browser
            services.AddCors(options =>
            {
                options.AddPolicy("allReady", AllReadyCorsPolicyFactory.BuildAllReadyOpenCorsPolicy());
            });

            // Add Entity Framework services to the services container.
            services.AddDbContext <AllReadyContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            Options.LoadConfigurationOptions(services, Configuration);

            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequiredLength         = 10;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireDigit           = true;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <AllReadyContext>()
            .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath        = "/Account/LogIn";
                options.AccessDeniedPath = "/Home/AccessDenied";
            });

            services.AddAuthentication();
            services.AddExternalAuthenticationServices(Configuration);
            services.AddAppAuthorization();
            services.AddAppLocalization();
            services.AddMemoryCache();

            // Add MVC services to the services container.
            // config add to get passed Angular failing on Options request when logging in.
            services.AddMvc(config =>
            {
                config.ModelBinderProviders.Insert(0, new AdjustToTimezoneModelBinderProvider());
            })
            .AddJsonOptions(options =>
                            options.SerializerSettings.ContractResolver = new DefaultContractResolver());

            // Adds a default in-memory implementation of IDistributedCache.
            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(20);
            });

            //Hangfire
            services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"]));

            services.AddScoped <IAllReadyUserManager, AllReadyUserManager>();
            services.AddScoped <IUserAuthorizationService, UserAuthorizationService>();
            services.AddScoped <IAuthorizableEventBuilder, AuthorizableEventBuilder>();

            // configure IoC support
            var container = AllReady.Configuration.Services.CreateIoCContainer(services, Configuration);

            return(container.Resolve <IServiceProvider>());
        }
예제 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Add CORS support.
            // Must be first to avoid OPTIONS issues when calling from Angular/Browser
            services.AddCors(options =>
            {
                options.AddPolicy("allReady", AllReadyCorsPolicyFactory.BuildAllReadyOpenCorsPolicy());
            });

            // Add Application Insights data collection services to the services container.
            services.AddApplicationInsightsTelemetry(Configuration);

            // Add Entity Framework services to the services container.
            services.AddDbContext <AllReadyContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            Options.LoadConfigurationOptions(services, Configuration);

            // Add Identity services to the services container.
            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequiredLength                    = 10;
                options.Password.RequireNonAlphanumeric            = false;
                options.Password.RequireDigit                      = true;
                options.Password.RequireUppercase                  = false;
                options.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/Home/AccessDenied");
            })
            .AddEntityFrameworkStores <AllReadyContext>()
            .AddDefaultTokenProviders();

            // Add Authorization rules for the app
            services.AddAuthorization(options =>
            {
                options.AddPolicy("OrgAdmin", b => b.RequireClaim(Security.ClaimTypes.UserType, "OrgAdmin", "SiteAdmin"));
                options.AddPolicy("SiteAdmin", b => b.RequireClaim(Security.ClaimTypes.UserType, "SiteAdmin"));
            });

            services.AddLocalization();

            //Currently AllReady only supports en-US culture. This forces datetime and number formats to the en-US culture regardless of local culture
            var usCulture         = new CultureInfo("en-US");
            var supportedCultures = new[] { usCulture };

            services.Configure <RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture(usCulture, usCulture);
                options.SupportedCultures     = supportedCultures;
                options.SupportedUICultures   = supportedCultures;
            });

            services.AddMemoryCache();

            // Add MVC services to the services container.
            // config add to get passed Angular failing on Options request when logging in.
            services.AddMvc(config =>
            {
                config.ModelBinderProviders.Insert(0, new AdjustToTimezoneModelBinderProvider());
            })
            .AddJsonOptions(options =>
                            options.SerializerSettings.ContractResolver = new DefaultContractResolver());

            // Adds a default in-memory implementation of IDistributedCache.
            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(20);
            });

            //Hangfire
            services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"]));

            // configure IoC support
            var container = AllReady.Configuration.Services.CreateIoCContainer(services, Configuration);

            return(container.Resolve <IServiceProvider>());
        }
예제 #3
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Add CORS support.
            // Must be first to avoid OPTIONS issues when calling from Angular/Browser
            services.AddCors(options =>
            {
                options.AddPolicy("allReady", AllReadyCorsPolicyFactory.BuildAllReadyOpenCorsPolicy());
            });

            // Add Entity Framework services to the services container.
            services.AddDbContext <AllReadyContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

            Options.LoadConfigurationOptions(services, Configuration);

            services.AddIdentity <ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequiredLength         = 10;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireDigit           = true;
                options.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <AllReadyContext>()
            .AddDefaultTokenProviders();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath        = "/Account/LogIn";
                options.AccessDeniedPath = "/Home/AccessDenied";
            });

            services.AddAuthentication();

            if (Configuration["Authentication:Facebook:AppId"] != null)
            {
                services.AddAuthentication()
                .AddFacebook(options => {
                    options.AppId     = Configuration["authentication:facebook:appid"];
                    options.AppSecret = Configuration["authentication:facebook:appsecret"];
                    options.BackchannelHttpHandler  = new FacebookBackChannelHandler();
                    options.UserInformationEndpoint = "https://graph.facebook.com/v2.5/me?fields=id,name,email,first_name,last_name";
                    options.Events = new OAuthEvents()
                    {
                        OnRemoteFailure = ctx => HandleRemoteLoginFailure(ctx)
                    };
                });
            }

            if (Configuration["Authentication:MicrosoftAccount:ClientId"] != null)
            {
                services.AddAuthentication()
                .AddMicrosoftAccount(options => {
                    options.ClientId     = Configuration["Authentication:MicrosoftAccount:ClientId"];
                    options.ClientSecret = Configuration["Authentication:MicrosoftAccount:ClientSecret"];
                    options.Events       = new OAuthEvents()
                    {
                        OnRemoteFailure = ctx => HandleRemoteLoginFailure(ctx)
                    };
                });
            }

            if (Configuration["Authentication:Twitter:ConsumerKey"] != null)
            {
                services.AddAuthentication()
                .AddTwitter(options => {
                    options.ConsumerKey         = Configuration["Authentication:Twitter:ConsumerKey"];
                    options.ConsumerSecret      = Configuration["Authentication:Twitter:ConsumerSecret"];
                    options.RetrieveUserDetails = true;
                    options.Events = new TwitterEvents()
                    {
                        OnRemoteFailure = ctx => HandleRemoteLoginFailure(ctx)
                    };
                });
            }

            if (Configuration["Authentication:Google:ClientId"] != null)
            {
                services.AddAuthentication()
                .AddGoogle(options => {
                    options.ClientId     = Configuration["Authentication:Google:ClientId"];
                    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                    options.Events       = new OAuthEvents()
                    {
                        OnRemoteFailure = ctx => HandleRemoteLoginFailure(ctx)
                    };
                });
            }

            // Add Authorization rules for the app
            services.AddAuthorization(options =>
            {
                options.AddPolicy(nameof(UserType.OrgAdmin), b => b.RequireClaim(ClaimTypes.UserType, nameof(UserType.OrgAdmin), nameof(UserType.SiteAdmin)));
                options.AddPolicy(nameof(UserType.SiteAdmin), b => b.RequireClaim(ClaimTypes.UserType, nameof(UserType.SiteAdmin)));
            });

            services.AddLocalization();

            //Currently AllReady only supports en-US culture. This forces datetime and number formats to the en-US culture regardless of local culture
            var usCulture         = new CultureInfo("en-US");
            var supportedCultures = new[] { usCulture };

            services.Configure <RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture(usCulture, usCulture);
                options.SupportedCultures     = supportedCultures;
                options.SupportedUICultures   = supportedCultures;
            });

            services.AddMemoryCache();

            // Add MVC services to the services container.
            // config add to get passed Angular failing on Options request when logging in.
            services.AddMvc(config =>
            {
                config.ModelBinderProviders.Insert(0, new AdjustToTimezoneModelBinderProvider());
            })
            .AddJsonOptions(options =>
                            options.SerializerSettings.ContractResolver = new DefaultContractResolver());

            // Adds a default in-memory implementation of IDistributedCache.
            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(20);
            });

            //Hangfire
            services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration["Data:HangfireConnection:ConnectionString"]));

            services.AddScoped <IAllReadyUserManager, AllReadyUserManager>();
            services.AddScoped <IUserAuthorizationService, UserAuthorizationService>();
            services.AddScoped <IAuthorizableEventBuilder, AuthorizableEventBuilder>();

            // configure IoC support
            var container = AllReady.Configuration.Services.CreateIoCContainer(services, Configuration);

            return(container.Resolve <IServiceProvider>());
        }