public async Task InvokeAsync(HttpContext httpContext, IStudentDbService service) { httpContext.Request.EnableBuffering(); if (httpContext.Request != null) { string sciezka = httpContext.Request.Path; string metoda = httpContext.Request.Method.ToString(); string querystring = httpContext.Request?.QueryString.ToString(); string bodyStr = ""; using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStr = await reader.ReadToEndAsync(); } using (System.IO.StreamWriter file = new System.IO.StreamWriter("requestsLog.txt", true)) { string text = String.Concat("[", DateTime.Now, "] Metoda: ", metoda, ", Sciezka: ", sciezka, ", Cialo: ", bodyStr, ", QueryString: ", querystring); file.WriteLine(text); }; } await _next(httpContext); }
public StudentsController( IStudentDbService studentDbService, IAccountService accountService) { this.studentDbService = studentDbService; this.accountService = accountService; }
public async Task InvokeAsync(HttpContext httpContext, IStudentDbService service) { httpContext.Request.EnableBuffering(); if (httpContext.Request != null) { string metoda = httpContext.Request.Method.ToString(); string sciezka = httpContext.Request.Path; string bodyStr = ""; string querystring = httpContext.Request?.QueryString.ToString(); using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStr = await reader.ReadToEndAsync(); } using (StreamWriter outputFile = new StreamWriter(Path.Combine("requestsLog.txt"), true)) { outputFile.WriteLine($"{metoda} | {sciezka} \nbody | {bodyStr} \nquery| {querystring}\n"); } } await _next(httpContext); }
public async Task InvokeAsync(HttpContext httpContext, IStudentDbService service) { httpContext.Request.EnableBuffering(); if (httpContext.Request != null) { string sciezka = httpContext.Request.Path; // api/students string querystring = httpContext.Request?.QueryString.ToString(); string metoda = httpContext.Request.Method; string body = ""; using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { body = await reader.ReadToEndAsync(); httpContext.Request.Body.Position = 0; } string query = httpContext.Request?.QueryString.ToString(); using (StreamWriter writer = new StreamWriter(Path.Combine("requestLog.txt"), true)) { writer.WriteLine($"{metoda} | {sciezka} | {body} | {query}"); } } await _next(httpContext); }
public async Task InvokeAsync(HttpContext context, IStudentDbService studentDbService) { context.Request.EnableBuffering(); if (context.Request != null) { string path = context.Request.Path; string method = context.Request.Method; string queryString = context.Request.QueryString.ToString(); string bodyString = ""; using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyString = await reader.ReadToEndAsync(); context.Request.Body.Position = 0; } using (var writer = new StreamWriter("requestsLog.txt", true)) { writer.WriteLine($"Log: {DateTime.Now}"); writer.WriteLine($"Method: {method}"); writer.WriteLine($"Path: {path}"); writer.WriteLine($"Body: {bodyString}"); writer.WriteLine($"QueryString: {queryString}"); } } if (_next != null) { await _next(context); } }
public async Task InvokeAsync(HttpContext httpContext, IStudentDbService service) { httpContext.Request.EnableBuffering(); if (httpContext.Request != null) { string path = httpContext.Request.Path; string querystring = httpContext.Request?.QueryString.ToString(); string method = httpContext.Request.Method.ToString(); string bodyStr = ""; using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStr = await reader.ReadToEndAsync(); //httpContext.Request.Body.Position = 0; } string reqLogpath = Directory.GetCurrentDirectory() + "/RequestLogs/" + "requestsLog.txt";//@"C:\Users\User\Desktop\requestsLog.txt"; using (var writer = new StreamWriter(reqLogpath, true, Encoding.UTF8)) { await writer.WriteLineAsync(method + " " + path + " " + bodyStr + " " + querystring); } } httpContext.Request.Body.Seek(0, SeekOrigin.Begin); await _next(httpContext); }
public async Task InvokeAsync(HttpContext context, IStudentDbService service) { context.Request.EnableBuffering(); if (context.Request != null) { string path = context.Request.Path; // api/students string method = context.Request.Method; string queryString = context.Request.QueryString.ToString(); string bodyStr = ""; using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStr = await reader.ReadToEndAsync(); context.Request.Body.Position = 0; } // zapisac do pliku // zapisac do bazy danych } if (_next != null) { await _next(context); } }
// 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(); } //log //app.UseMiddleware<ExceptionMiddleware>(); app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); // loggingMiddleware app.UseMiddleware <LoggingMiddleware>(); //app.UseWhen(context => context.Request.Path.ToString().Contains("secret"), app => app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Musisz podac numer indeksu"); return; } string index = context.Request.Headers["Index"].ToString(); var student = service.GetStudent(index); if (student != null) { await context.Response.WriteAsync("jest student"); } else { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("brak studenta"); return; } await next(); }); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public BasicAuthHandler( IOptionsMonitor <AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IStudentDbService service ) : base(options, logger, encoder, clock) { }
public EnrollmentDbService( IStudyDbService studyDbService, IStudentDbService studentDbService, APBDContext context) { this.studyDbService = studyDbService; this.studentDbService = studentDbService; this.context = context; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService dbservice) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); //Zgodnie z wyk³adem nie chcemy robiæ logów z dokumentacji app.UseMiddleware <LoggingMiddleware>(); app.UseMiddleware <ExceptionMiddleware>(); app.UseMiddleware <LoggingMiddleware>(); app.UseWhen(context => context.Request.Path.ToString().Contains("secret"), app => { app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("W naglowku potrzebny indeks studenta"); return; } var index = context.Request.Headers["Index"].ToString(); var student = dbservice.getStudent(index); if (student == null) { context.Response.StatusCode = StatusCodes.Status204NoContent; context.Response.WriteAsync("Brak takiego studenta"); } await next(); }); }); app.UseHttpsRedirection(); 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.UseSwagger(); //app.UseSwaggerUI(c => //{ // c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); //}); //app.UseMiddleware<LoggingMiddleware>(); //app.UseHttpsRedirection(); //app.UseAuthorization(); /* * app.Use(async (context, next) => * { * if (!context.Request.Headers.ContainsKey("Index")) * { * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Nie podales numeru indeksu"); * return; * } * * string index = context.Request.Headers["Index"].ToString(); * Boolean check = service.CheckIndexNumber(index); * * if (!check) * { * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Nie znaleziono studenta o podanym numerze indeksu"); * 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.UseHttpsRedirection(); app.UseRouting(); app.UseMiddleware <LoggingMiddleware>(); app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Brak indeksu"); return; } string index = context.Request.Headers["Index"].ToString(); //check in db string connString = "Data Source=db-mssql;Initial Catalog=s18891;Integrated Security=True"; using (var connection = new SqlConnection(connString)) using (var command = new SqlCommand()) { command.Connection = connection; connection.Open(); //Check if student exists command.CommandText = "select * from Student where IndexNumber=" + index; var dr = command.ExecuteReader(); if (!dr.Read()) { await context.Response.WriteAsync("Student o podanym indeksie nie istnieje"); return; } } await next(); }); /// 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(); } //Middleware obs³uguj¹cy b³êdy app.UseMiddleware <ExceptionMiddleware>(); //dodawanie dokumentacji (2/2) app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); //Middleware loguj¹cy ¿¹dania app.UseMiddleware <LoggingMiddleware>(); //Middleware - Index: s***** -> db app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Nale¿y podaæ numer indexu"); return; //short circuit, od razu zwracana odpowiedŸ, nie przekazywana do kolejnego Middleware } string index = context.Request.Headers["Index"].ToString(); bool studentExists = service.StudentExists(index); if (!studentExists) { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("Student " + index + " nie istnieje, brak autoryzacji"); return; } context.Response.StatusCode = StatusCodes.Status202Accepted; await next(); }); app.UseRouting(); 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.UseHttpsRedirection(); //v1,v2 rozne wersje naszego api //rejestracja middlewaru swagger do generowania i samego interjfejsu app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students app API"); } ); //dodawanie middleware app.UseMiddleware <LoggingMiddleware>(); app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Podaj numer indeksu"); return;//bezposredni zwrot odpowiedzi } string index = context.Request.Headers["Index"].ToString(); var student = service.GetStudent(index); if (student == null) { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("Student nie znaleziony"); return; } await next(); }); app.UseRouting(); 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) { app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); /* * app.UseMiddleware<LoggingMiddleware>(); * app.Use(async (context, next) => * { * * if (!context.Request.Headers.ContainsKey("IndexNumber")) * { * * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Musisz podac numer indexu istniejacego studenta"); * return; * * } * * * * var index = context.Request.Headers["IndexNumber"]; * * Student student = service.GetStudent(index); * * if (student == null) * { * await context.Response.WriteAsync("Nie ma takiego studenta."); * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * return; * } * await context.Response.WriteAsync(student.ToString()); * * 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.UseHttpsRedirection(); //Middleware - Index: sxxxxx ->DB? app.UseMiddleware <ExceptionMiddleware>(); app.UseMiddleware <LoggingMiddleware>(); //app.Use(async (context, next) => //{ // if (!context.Request.Headers.ContainsKey("Index")) // { // context.Response.StatusCode = StatusCodes.Status401Unauthorized; // await context.Response.WriteAsync("Musisz podac numer indeksu"); // return; // } // string index = context.Request.Headers["Index"].ToString(); // var stud = service.GetStudent(index); // if (stud == null) // { // context.Response.StatusCode = StatusCodes.Status404NotFound; // await context.Response.WriteAsync("Index nie istnieje"); // return; // } // await next(); //}); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); //app.Use(async (context, c) => //{ // context.Response.Headers.Add("Secret", "1234"); // await c.Invoke(); //}); //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<ExceptionMiddleware>(); //generowanie dokumentacji krok 2 app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); //app.UseMiddleware<LoggingMiddleware>(); 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; } string index = context.Request.Headers["Index"].ToString(); Student stud = service.GetStudent(index); if (stud == null) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Brak studenta o podanym indeksie"); 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.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); app.UseWhen(context => context.Request.Path.ToString().Contains("secured"), app => { app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("index number missing"); return; } var index = context.Request.Headers["Index"].ToString(); var stud = service.GetStudent(index); if (stud == null) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync($"User ({index}) not found"); return; } await next(); }); }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseMiddleware <LoggingMiddleware>(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService dbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students API"); }); // 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."); * return; * } * * var index = context.Request.Headers["Index"].ToString(); * * if (!dbService.checkIndex(index)) * { * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Podany indeks nie istnieje w bazie."); * 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.UseHttpsRedirection(); app.UseMiddleware <LoggingMiddleware>(); /* * app.Use(async (contex, next) => * { * if (!contex.Request.Headers.ContainsKey("Index")) * { * contex.Response.StatusCode = StatusCodes.Status401Unauthorized; * await contex.Response.WriteAsync("Nie podano indeksu w nag³ówku"); * return; * } * String index = contex.Request.Headers["Index"].ToString(); * var student = service.GetStudent(index); * if (student.IndexNumber == null) * { * contex.Response.StatusCode = StatusCodes.Status401Unauthorized; * await contex.Response.WriteAsync("Brak studenta w bazie"); * return; * } * else * { * await contex.Response.WriteAsync("Student znajduje siê w bazie"); * } * * 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.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Students api V1"); }); app.UseWhen(context => context.Request.Path.ToString().Contains("index"), app => { app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("IndexNumber")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("No index 1"); return; } string index = context.Request.Headers["IndexNumber"].ToString(); var student = service.GetStudent(index); if (student == null) { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("Student not found."); 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(); } //Middleware app.UseMiddleware <ExceptionMiddleware>(); app.UseMiddleware <LoggingMiddleware>(); /* * app.Use(async (context, next) => * { * if (!context.Request.Headers.ContainsKey("Index")) * { * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Podaj numer indeksu"); * return; * } * string index = context.Request.Headers["Index"].ToString(); * * var stud = service.GetStudent(index); * if (stud == null) * { * context.Response.StatusCode = StatusCodes.Status404NotFound; * await context.Response.WriteAsync("Nie znaleziono studenta"); * 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 studentDbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Use(async(context, next) => { var CzyJestIndex = context.Request.Headers.ContainsKey("Index"); if (CzyJestIndex == null) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("BRAK INDEXU"); return; } else { String PobranyIndex = context.Request.Headers["Index"].ToString(); var stud = studentDbService.GetStudent(index); if (stud) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("BRAK TAKIEGO STUDENTA"); return; } } await next(); }); app.UseRouting(); app.UseAuthorization(); app.UseMiddleware <LoggingMiddleware>(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService studentDbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); //app.UseMiddleware<LoggingMiddleware>(); /* * app.Use(async (context, next) => * { * if (!context.Request.Headers.ContainsKey("Index")) * { * context.Response.StatusCode = StatusCodes.Status401Unauthorized; * await context.Response.WriteAsync("Musisz podac numer indeksu"); * return; * } * * string index = context.Request.Headers["Index"].ToString(); * var student = studentDbService.GetStudent(index); * if (student == null) * { * context.Response.StatusCode = StatusCodes.Status404NotFound; * await context.Response.WriteAsync("Student not found"); * return; * } * * await next(); * });*/ app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService dbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentDbService dbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Student App API"); }); // 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"); // return; // } // // string index = context.Request.Headers["Index"].ToString(); // // if (!dbService.CheckIndexNumber(index)) // { // context.Response.StatusCode = StatusCodes.Status401Unauthorized; // await context.Response.WriteAsync("Student o podanym indeksie nie istnieje"); // 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) { //Dodawanie dokumentacji cd app.UseSwagger(); app.UseSwaggerUI(config => { config.SwaggerEndpoint("/swagger/v1/swagger.json", "Students App API"); }); app.UseMiddleware <LoggingMiddleware>(); app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Musisz podac numer indeksu."); return; //short circuit } string index = context.Request.Headers["Index"].ToString(); //exists in database var stud = service.GetStudent(index); if (stud == null) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("W bazie nie istnieje student o podanym indeksie."); return; } await next(); }); app.UseRouting(); //decyduje jaki kotroler i metoda maja obsluzyc zapytanie app.UseAuthentication(); app.UseAuthorization(); //sprawdza uprawnienia app.UseEndpoints(endpoints => //new StudentsController(?).GetStudents() -> Response { 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 dbService) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMiddleware <LoggingMiddleware>(); app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("IndexNumber")) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Nie podano indeksu"); return; } var bodyStream = string.Empty; using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStream = await reader.ReadToEndAsync(); } //³¹czenie z baz¹ danych string index = context.Request.Headers["IndexNumber"].ToString(); if (!dbService.CheckIndexNumber(index)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("osoba o podanym indeksie nie istnieje"); 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 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("Musisz podac numer indeksu"); return; } string index = context.Request.Headers["Index"].ToString(); Console.WriteLine(index); Student student = dbService.GetStudent(index); if (student == null) { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("Nie znaleziono studenta o indeksie " + index); return; } context.Response.StatusCode = StatusCodes.Status200OK; await next(); }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }