// This method gets called by a runtime. Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { var loggerFactory = LoggerFactory.Create(builder => { builder.AddDebug(); builder.SetMinimumLevel(LogLevel.Information); }); services.AddAuthorization(options => { options.AddPolicy(PolicyNames.RequireSurveyCreator, policy => { policy.AddRequirements(new SurveyCreatorRequirement()); policy.RequireAuthenticatedUser(); // Adds DenyAnonymousAuthorizationRequirement policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); }); options.AddPolicy(PolicyNames.RequireSurveyAdmin, policy => { policy.AddRequirements(new SurveyAdminRequirement()); policy.RequireAuthenticatedUser(); // Adds DenyAnonymousAuthorizationRequirement policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); }); }); // Add Entity Framework services to the services container. services.AddEntityFrameworkSqlServer() .AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetSection("Data")["SurveysConnectionString"])); services.AddScoped <TenantManager, TenantManager>(); services.AddScoped <UserManager, UserManager>(); services.AddControllers(); services.AddScoped <ISurveyStore, SqlServerSurveyStore>(); services.AddScoped <IQuestionStore, SqlServerQuestionStore>(); services.AddScoped <IContributorRequestStore, SqlServerContributorRequestStore>(); services.AddSingleton <IAuthorizationHandler>(factory => { var loggerFactory = factory.GetService <ILoggerFactory>(); return(new SurveyAuthorizationHandler(loggerFactory.CreateLogger <SurveyAuthorizationHandler>())); }); services.AddHttpContextAccessor(); var configOptions = new AppConfiguration.ConfigurationOptions(); Configuration.Bind(configOptions); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Audience = configOptions.AzureAd.WebApiResourceId; options.Authority = Constants.AuthEndpointPrefix; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false }; options.Events = new SurveysJwtBearerEvents(loggerFactory.CreateLogger <SurveysJwtBearerEvents>()); }); }
// Configure is called after ConfigureServices is called. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext, ILoggerFactory loggerFactory) { var configOptions = new AppConfiguration.ConfigurationOptions(); Configuration.Bind(configOptions); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } app.UseJwtBearerAuthentication(new JwtBearerOptions { Audience = configOptions.AzureAd.WebApiResourceId, Authority = Constants.AuthEndpointPrefix, TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuer = false }, Events = new SurveysJwtBearerEvents(loggerFactory.CreateLogger <SurveysJwtBearerEvents>()) }); // Add MVC to the request pipeline. app.UseMvc(); }
// Configure is called after ConfigureServices is called. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext, ILoggerFactory loggerFactory) { var configOptions = new AppConfiguration.ConfigurationOptions(); Configuration.Bind(configOptions); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } app.UseJwtBearerAuthentication(new JwtBearerOptions { Audience = configOptions.AzureAd.WebApiResourceId, Authority = Constants.AuthEndpointPrefix, TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuer = false }, Events= new SurveysJwtBearerEvents(loggerFactory.CreateLogger<SurveysJwtBearerEvents>()) }); // Add MVC to the request pipeline. app.UseMvc(); }