示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(builder =>
                                builder.AddConfiguration(this.Configuration.GetSection("Logging")).AddConsole());
            services.Configure <ArchiveDbConfiguration>(this.Configuration.GetSection("ArchiveDb"));
            services.AddDbContext <ArchiveDbContext>();
            services.AddControllers()
            .AddNewtonsoftJson(options => {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver {
                    // Use TitleCase naming everywhere so that we're consistent with OData endpoints
                    NamingStrategy = new DefaultNamingStrategy()
                };
            });
            services.AddMvc();
            services.AddOData();

            // wire up azure cognitive config
            var azureConfiguration = new AzureCognitiveConfiguration();

            this.Configuration.GetSection("Azure").Bind(azureConfiguration);
            services.AddSingleton(azureConfiguration);

            // allow cognitive service to be injectable
            services.AddScoped <CognitiveService>();

            var originConfiguration = new OriginPolicyConfiguration();

            this.Configuration.GetSection("OriginPolicy").Bind(originConfiguration);
            services.AddSingleton(Microsoft.Extensions.Options.Options.Create(originConfiguration));
            if (originConfiguration.HasOrigin())
            {
                services.AddCors(options => {
                    options.AddDefaultPolicy(
                        builder => {
                        builder
                        .SetIsOriginAllowed(origin =>
                                            IsGlobMatch(originConfiguration.Allow, origin)
                                            /* originConfiguration.AllowOrigin.Any(pattern => IsGlobMatch(pattern, origin)) */)
                        .AllowAnyHeader()
                        .AllowAnyMethod();
                    });
                });
            }

            // Register Commands
            services.AddScoped <InitializeCommand>();
        }
示例#2
0
        internal void ConfigureServices(IServiceCollection services, Boolean skipHosting)
        {
            services.AddLogging(builder =>
                                builder.AddConfiguration(this.Configuration.GetSection("Logging")).AddConsole());

            // Add Application Dependencies Here
            services.Configure <ArchiveDbConfiguration>(this.Configuration.GetSection("ArchiveDb"));
            services.AddDbContext <ArchiveDbContext>();

            if (!skipHosting)
            {
                // Add Hosting specific Dependencies Here

                // Wire up Azure Cognitive config
                var azureConfiguration = new AzureCognitiveConfiguration();
                this.Configuration.GetSection("Azure").Bind(azureConfiguration);
                services.AddSingleton(azureConfiguration);

                services.AddScoped <ICloudOcrService, CognitiveService>();

                services.AddScoped <ILoginLogger, DbLoginLogger>();

                var facebookConfig = new FacebookConfiguration();
                this.Configuration.GetSection("Facebook").Bind(facebookConfig);
                services.AddSingleton(Options.Create(facebookConfig));

                var authenticationBuilder =
                    services
                    .AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; })
                    .AddCookie(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        options => {
                    options.Events.OnRedirectToLogin = context => {
                        context.Response.StatusCode = 401;
                        return(Task.CompletedTask);
                    };
                });

                if (!String.IsNullOrEmpty(facebookConfig.ApplicationId))
                {
                    Console.Error.WriteLine($"Enabling Facebook Login with ApplicationId: {facebookConfig.ApplicationId}");
                    authenticationBuilder = authenticationBuilder.AddFacebook(facebookOptions => {
                        facebookOptions.AppId            = facebookConfig.ApplicationId;
                        facebookOptions.AppSecret        = facebookConfig.Secret;
                        facebookOptions.CallbackPath     = "/api/auth/facebook-login";
                        facebookOptions.AccessDeniedPath = "/api/auth/facebook-access-denied";
                        facebookOptions.Fields.Add("email");
                        facebookOptions.Fields.Add("first_name");
                        facebookOptions.Fields.Add("last_name");
                        facebookOptions.Events.OnTicketReceived = context => {
                            var loginLogger = context.HttpContext.RequestServices.GetRequiredService <ILoginLogger>();
                            return(loginLogger.LogLogin(context.Principal));
                        };
                    });
                }
                else
                {
                    Console.Error.WriteLine("Facebook configuration not found. Facebook login will not be enabled.");
                }

                // Initialized by UserContextMiddleware for each requests
                services.AddScoped <UserContext>();

                var originConfiguration = new OriginPolicyConfiguration();
                this.Configuration.GetSection("OriginPolicy").Bind(originConfiguration);
                services.AddSingleton(Options.Create(originConfiguration));
                if (originConfiguration.HasOrigin())
                {
                    services.AddCors(options => {
                        options.AddDefaultPolicy(
                            builder => {
                            builder
                            .SetIsOriginAllowed(origin =>
                                                IsGlobMatch(originConfiguration.Allow, origin))
                            .AllowCredentials()
                            .AllowAnyHeader()
                            .AllowAnyMethod();
                        });
                    });
                }

                // Asp.Net MVC Dependencies
                services
                .AddControllers(options => {
                    var policy = new AuthorizationPolicyBuilder()
                                 .RequireAuthenticatedUser()
                                 .Build();

                    if (!String.IsNullOrEmpty(facebookConfig.ApplicationId))
                    {
                        options.Filters.Add(new AuthorizeFilter(policy));
                    }
                })
                .AddNewtonsoftJson(options => {
                    options.SerializerSettings.ContractResolver = new DefaultContractResolver {
                        // Use TitleCase naming everywhere so that we're consistent with OData endpoints
                        NamingStrategy = new DefaultNamingStrategy()
                    };
                    options.SerializerSettings.Converters.Add(new StringEnumConverter());
                });

                services.AddMvc();
                services.AddOData();
            } // End of Hosting Specific Dependencies

            // Register Commands
            services.AddScoped <InitializeCommand>();
        }