Example #1
0
        public async Task InvokeAsync(HttpContext context, IStudentsDbService studentsDbService)
        {
            if (!context.Request.Headers.ContainsKey("Index"))
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.WriteAsync("No Index number entered");

                return;
            }

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

            if (!studentsDbService.CheckIfStudentExists(index))
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.WriteAsync("Unauthorized access");

                return;
            }

            if (_next != null)
            {
                await _next(context);
            }
        }
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 db)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Use(async(context, next) => {
                if (!context.Request.Headers.ContainsKey("Index"))
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    await context.Response.WriteAsync("Musisz podac nr indeksu");
                    return;
                }
                else
                {
                    string index = context.Request.Headers["Index"].ToString();

                    if (!db.CheckIfStudentExists(index))
                    {
                        context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                        await context.Response.WriteAsync("Nie ma takiego studenta");
                        return;
                    }
                }

                await next();
            });

            app.UseMiddleware <LoggingMiddleware>();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

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