Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var connectionString = Configuration.GetConnectionString("DefaultConnection");

            // Config
            var hangfireConfiguration = new HangFireConfiguration();

            Configuration.Bind("Hangfire", hangfireConfiguration);
            services.AddSingleton(hangfireConfiguration);

            var listPaginationConfiguration = new ListPaginationConfiguration();

            Configuration.Bind("ListPagination", listPaginationConfiguration);
            services.AddSingleton(listPaginationConfiguration);

            var traktApiConfiguration = new TraktAPIConfiguration();

            Configuration.Bind("Trakt", traktApiConfiguration);
            services.AddSingleton(traktApiConfiguration);

            var githubApiConfiguration = new GithubAPIConfiguration();

            Configuration.Bind("GitHub", githubApiConfiguration);
            services.AddSingleton(githubApiConfiguration);

            var limitConfigurationList = new LimitConfigurationList();

            Configuration.Bind("LimitConfig", limitConfigurationList);
            services.AddSingleton(limitConfigurationList);

            var userMappingConfigurationList = new UserMappingConfigurationList();

            Configuration.Bind("UserMappingConfig", userMappingConfigurationList);
            services.AddSingleton(userMappingConfigurationList);


            // Multi Instance LB
            services.AddDbContext <DataProtectionDbContext>(options =>
                                                            options.UseSqlServer(connectionString)
                                                            );
            services.AddDataProtection()
            .PersistKeysToDbContext <DataProtectionDbContext>()
            .SetApplicationName("Listrr");
            services.AddDistributedSqlServerCache(options =>
            {
                options.ConnectionString = connectionString;
                options.SchemaName       = "dbo";
                options.TableName        = "Cache";
            });

            services.Configure <CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddDbContext <AppDbContext>(options =>
                                                 options.UseSqlServer(connectionString)
                                                 );
            services.AddDefaultIdentity <User>(options =>
            {
                options.User.AllowedUserNameCharacters = null;
            }).AddEntityFrameworkStores <AppDbContext>();
            services.AddHangfire(x =>
                                 x.UseSqlServerStorage(connectionString));

            services.AddAuthentication()
            .AddTrakt(options =>
            {
                options.ClientId     = traktApiConfiguration.ClientId;
                options.ClientSecret = traktApiConfiguration.ClientSecret;
                options.SaveTokens   = true;

                options.ClaimActions.MapJsonSubKey(Constants.Trakt_Claim_Ids_Slug, "ids", "slug");

                options.Events.OnCreatingTicket = ctx =>
                {
                    List <AuthenticationToken> tokens = ctx.Properties.GetTokens() as List <AuthenticationToken>;
                    tokens.Add(new AuthenticationToken()
                    {
                        Name  = "TicketCreated",
                        Value = DateTime.Now.ToString()
                    });
                    ctx.Properties.StoreTokens(tokens);
                    return(Task.CompletedTask);
                };
            })
            .AddGitHub(options =>
            {
                options.ClientId                = githubApiConfiguration.ClientId;
                options.ClientSecret            = githubApiConfiguration.ClientSecret;
                options.SaveTokens              = true;
                options.Events.OnCreatingTicket = ctx =>
                {
                    List <AuthenticationToken> tokens = ctx.Properties.GetTokens() as List <AuthenticationToken>;
                    tokens.Add(new AuthenticationToken()
                    {
                        Name  = "TicketCreated",
                        Value = DateTime.Now.ToString()
                    });
                    ctx.Properties.StoreTokens(tokens);
                    return(Task.CompletedTask);
                };
            });

            services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.Name     = "Listrr";
                options.Cookie.HttpOnly = true;
                options.ExpireTimeSpan  = TimeSpan.FromHours(1);

                options.LoginPath         = "/Identity/Account/Login";
                options.AccessDeniedPath  = "/Identity/Account/AccessDenied";
                options.SlidingExpiration = true;
            });

            services.AddHttpContextAccessor();

            //ReverseProxy Fix
            services.Configure <ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });

            services.AddScoped <IGitHubGraphService, GitHubGraphService>();
            services.AddScoped <IBackgroundJobQueueService, BackgroundJobQueueService>();
            services.AddScoped <ITraktListRepository, TraktListRepository>();
            services.AddScoped <ITraktMovieRepository, TraktMovieRepository>();
            services.AddScoped <ITraktShowRepository, TraktShowRepository>();
            services.AddScoped <ITraktCodeRepository, TraktCodeRepository>();
            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <ITraktService, TraktService>();

            services.AddControllersWithViews();
            services.AddRazorPages();
        }
Ejemplo n.º 2
0
 public HomeController(ITraktListRepository traktListRepository, ListPaginationConfiguration listPaginationConfiguration)
 {
     _traktListRepository         = traktListRepository;
     _listPaginationConfiguration = listPaginationConfiguration;
 }