Esempio n. 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <EventsDbContext>(
                options => options.UseSqlServer(
                    Configuration.GetConnectionString("EventManagement")));
            services.AddTransient <EventsDbInitializer>();

            services.Configure <RouteOptions>(options =>
            {
                // Generated path urls should be lowercase.
                options.LowercaseUrls = true;
            });

            services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddApplicationPart(typeof(AccountController).Assembly)
            .AddJsonOptions(options =>
            {
                // Important: ASP.NET Core is serializing dates to JSON as local time.
                options.SerializerSettings.DateTimeZoneHandling  = DateTimeZoneHandling.Utc;
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

            services.AddWebOptimizer(pipeline =>
            {
                pipeline.AddLessBundle("css/site.css", "css/site.less");
                pipeline.AddLessBundle("css/ticket-validation.css", "css/ticket-validation.less");
            });

            services.AddIdentityServer()
            .AddDeveloperSigningCredential(persistKey: true)
            .AddInMemoryApiResources(IdentityServerConfig.GetApis())
            .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
            .AddClientStore <EventManagementLocalClientStore>()
            .AddProfileService <UserProfileService>();
            services.AddTransient <IUserStore, UserStore>();

            // Configure authentication to protect our web api.
            services.AddAuthentication()
            .AddLocalApi(Constants.JwtAuthScheme, options =>
            {
                options.ExpectedScope = "eventmanagement.admin";
            });

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

            services.AddSwaggerDocument();
            services.AddAutoMapper(GetType());
        }
Esempio 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 <EventsDbContext>(
                options => options.UseSqlServer(
                    Configuration.GetConnectionString("EventManagement")));

            services.AddTransient <EventsDbContextSeed>();
            services.AddTransient <EventManagementLocalClientStore>();
            services.TryAddTransient <IUserStore, DatabaseUserStore>();
            services.TryAddTransient <IEventManagementClientStore, DatabaseClientStore>();
            services.TryAddTransient <ITicketsRepository, TicketsRepository>();
            services.TryAddTransient <ITicketDeliveryDataRepository, TicketDeliveryDataRepository>();
            services.TryAddTransient <IAuditEventLog, AuditEventLog>();
            services.TryAddTransient <IEmailService, EmailService>();
            services.TryAddTransient <ITicketNumberService, TicketNumberService>();
            services.TryAddTransient <ITicketDeliveryService, TicketDeliveryService>();
            services.TryAddTransient <IPdfTicketService, PdfTicketService>();
            services.TryAddTransient <ITicketRedirectService, TicketRedirectService>();

            services.AddIdentityServer()
            .AddDeveloperSigningCredential(persistKey: true)
            .AddInMemoryApiResources(IdentityServerConfig.GetApis())
            .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
            .AddClientStore <EventManagementClientStore>()
            .AddProfileService <UserProfileService>();

            services.TryAddTransient <IJwtTokenService, JwtTokenService>();

            services.AddHangfire(configuration => configuration
                                 .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                                 .UseSimpleAssemblyNameTypeSerializer()
                                 .UseRecommendedSerializerSettings()
                                 .UseSqlServerStorage(Configuration.GetConnectionString("EventManagement"), new SqlServerStorageOptions
            {
                CommandBatchMaxTimeout       = TimeSpan.FromMinutes(5),
                SlidingInvisibilityTimeout   = TimeSpan.FromMinutes(5),
                QueuePollInterval            = TimeSpan.Zero,
                UseRecommendedIsolationLevel = true,
                UsePageLocksOnDequeue        = true,
                DisableGlobalLocks           = true
            })
                                 .UseConsole()
                                 .UseFilter(new JobContext()));

            // Add the processing server as IHostedService
            services.AddHangfireServer();

            // Custom authorization filter for the Hangfire Dashboard.
            services.AddTransient <BackgroundJobsDashboardAuthorizationFilter>();

            services.Configure <RouteOptions>(options =>
            {
                // Generated path urls should be lowercase.
                options.LowercaseUrls = true;
            });

            services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddApplicationPart(typeof(AccountController).Assembly)
            .AddJsonOptions(options =>
            {
                // Important: ASP.NET Core is serializing dates to JSON as local time.
                options.SerializerSettings.DateTimeZoneHandling  = DateTimeZoneHandling.Utc;
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

            services.AddWebOptimizer(pipeline =>
            {
                pipeline.AddLessBundle("css/site.css", "css/site.less");
                pipeline.AddLessBundle("css/ticket-validation.css", "css/ticket-validation.less");
                pipeline.AddLessBundle("css/conference-dialog.css", "conference-dialog/styles.less");

                var confDialogBundler = pipeline
                                        .AddBundle("js/conference-dialog.js",
                                                   "text/javascript; charset=UTF-8",
                                                   "lib/jquery/jquery.min.js",
                                                   "lib/handlebars/handlebars.min.js",
                                                   "conference-dialog/main.js")
                                        .Concatenate();

                if (!Environment.IsDevelopment())
                {
                    confDialogBundler.MinifyJavaScript();
                }
            });

            // Configure authentication to protect our web api.
            services
            .AddAuthentication()
            .AddLocalApi(options =>
            {
                options.ExpectedScope = AdminApi.ScopeName;
            })
            .AddCookie(MasterQrCode.AuthenticationScheme, options =>
            {
                options.Cookie.HttpOnly   = true;
                options.Cookie.Expiration = TimeSpan.FromDays(1);
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy(AdminApi.PolicyName, policy =>
                {
                    policy.AddAuthenticationSchemes(
                        IdentityServerConstants.DefaultCookieAuthenticationScheme,
                        IdentityServerConstants.LocalApi.AuthenticationScheme);

                    policy.RequireAuthenticatedUser();
                });
            });

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

            services.AddOpenApiDocument(document =>
            {
                document.PostProcess = doc =>
                {
                    doc.Info.Title = "Event Management API";
                };

                document.DocumentProcessors.Add(
                    new SecurityDefinitionAppender(
                        "bearer", Enumerable.Empty <string>(),
                        new OpenApiSecurityScheme
                {
                    Type  = OpenApiSecuritySchemeType.OAuth2,
                    Flow  = OpenApiOAuth2Flow.Implicit,
                    Flows = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            Scopes = new Dictionary <string, string>
                            {
                                { AdminApi.ScopeName, AdminApi.DisplayName },
                            },
                            AuthorizationUrl = "/connect/authorize",
                            TokenUrl         = "/connect/token"
                        },
                        ClientCredentials = new OpenApiOAuthFlow
                        {
                            Scopes = new Dictionary <string, string>
                            {
                                { AdminApi.ScopeName, AdminApi.DisplayName },
                            },
                            TokenUrl = "/connect/token"
                        }
                    },
                }));

                document.OperationProcessors.Add(
                    new AspNetCoreOperationSecurityScopeProcessor("bearer"));
            });
            services.AddAutoMapper(GetType());
        }