// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService studentDbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMiddleware <LoggingMiddleware>(); app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Nie podano indeksu w nag³ówku"); return; } var index = context.Request.Headers["Index"].ToString(); if (!studentDbService.CheckIndex(index)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Student nie istnieje w bazie danych"); return; } await next(); }); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService service) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMiddleware<LoggingMiddleware>(); app.Use(async (context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Must provide index"); return; } string index = context.Request.Headers["Index"].ToString(); if (!service.CheckIndex(index)) { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("No such index in database"); return; } await next(); }); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }