Example #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService serv)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            // odt¹d
            //app.UseHttpsRedirection();
            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;
                }

                //to najpierw
                // plik z generowym plikiem middleware byl w folderze middleware albo bezposrednio do katalgu projektu (oprocz
                // debug katalogu ) - musi byc dopisywany a nie tworzony
                //var bodyStream = string.Empty;
                //using (var reader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
                //{
                //    bodyStream = await reader.ReadToEndAsync();
                //}

                //HttpContext.Request.EnableBuffering(); /*(na pocz¹tku)*/

                //    HttpContext.Request.Body.Seek(0, SeekOrigin.Begin);
                //    //(na koñcu przed await _next...)

                var index = context.Request.Headers["Index"].ToString();

                if (!serv.CheckIndex(index)) // sprawdzenie czy student wystepuje w bazie danych
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Nie ma indeksu w bazie");
                    return;
                }

                await next();
            });

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService dbService)
        {
            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("No index found");
                    return;
                }
                else
                {
                    string index    = context.Request.Headers["Index"].ToString();
                    var indexExists = dbService.CheckIndex(index);
                    //This method is called in DBController
                    if (!indexExists)
                    {
                        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                        await context.Response.WriteAsync("Student with given index");
                        return;
                    }

                    /*else if (!indexExists)
                     * {
                     *  //In case index header exists but the student number is not a valid one
                     *  context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                     *  await context.Response.WriteAsync("No index found");
                     *  return;
                     * }*/
                }
                await next();
            });
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService service)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMiddleware <LoggingMiddleware>();

            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("Musi zawieraæ indeks");
                    return;
                }

                string index = context.Request.Headers["Index"].ToString();
                if (!service.CheckIndex(index))
                {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                    await context.Response.WriteAsync("Podany indeks nie istnieje!");
                    return;
                }

                await next();
            });

            app.UseRouting();


            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService dbService)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();
            app.UseSwaggerUI(config =>
            {
                config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API");
            });

            app.Use(async(context, next) =>
            {
                //Custom code
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Nie podales indeksu");
                    return;
                }

                string index = context.Request.Headers["Index"].ToString();

                if (!dbService.CheckIndex(index))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Nie ma takiego indeksu");
                    return;
                }

                await next();
            });

            app.UseMiddleware <LoggingMiddleware>();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Example #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentsDbService dbService)
        {
            app.UseMiddleware <LoggingMiddleware>();
            app.Use(async(context, next) =>
            {
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Index was not provided!");
                    return;
                }
                else
                {
                    string index = context.Request.Headers["Index"].ToString();
                    if (!dbService.CheckIndex(index))
                    {
                        context.Response.StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound;
                        await context.Response.WriteAsync(" Student with given index is not found!");
                        return;
                    }
                }
                await next();
            });


            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }