// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentServiceDb service) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Student API V1"); }); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public async Task InvokeAsync(HttpContext context, IStudentServiceDb service) { if (context.Request != null) { string path = context.Request.Path; // /api/enrollments string method = context.Request.Method; // GET, POST , PUT , DELETE string queryString = context.Request.QueryString.ToString(); //?name=Ahmad string bodyStr = ""; using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8, true, 1024, true)) { bodyStr = await reader.ReadToEndAsync(); } //save log: I could implenet the saving to log file over here but since in the future // I might want to save this log to the database i will implement it in IStudentService and inject it String request = "Path :" + path + "\nMethod: " + method + "\nQuery String: " + queryString + "\nBody: " + bodyStr; service.logRequest(request); } if (_next != null) { await _next(context); //run next middleware } }
public BasicAuthHandler( IOptionsMonitor <AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IStudentServiceDb service ) : base(options, logger, encoder, clock) { _service = service; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentServiceDb service) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Student API V1"); }); 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("Index number required"); * return; * } * * string index = context.Request.Headers["Index"].ToString(); * var st = service.IndexExists(index); * if (!st) * { * context.Response.StatusCode = StatusCodes.Status400BadRequest; * await context.Response.WriteAsync("Incorrect Index number"); * return; * } * * await next(); * });*/ app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentServiceDb service) { app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "APBD 6"); }); app.UseMiddleware <Logger>(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } 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.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public async Task InvokeAsync(HttpContext httpContext, IStudentServiceDb service) { if (httpContext.Request != null) { httpContext.Request.EnableBuffering(); string BodyBuffer = ""; using (StreamReader reader = new StreamReader(httpContext.Request.Body)) { BodyBuffer = await reader.ReadToEndAsync(); httpContext.Request.Body.Position = 0; } var log = new Log { Path = httpContext.Request.Path, Method = httpContext.Request.Method, Query = httpContext.Request.QueryString.ToString(), Body = BodyBuffer, Time = DateTime.UtcNow }; using (StreamWriter writer = File.AppendText("Log.txt")) { string LogString = "TIME : " + log.Time + "\n" + "PATH: " + log.Path + "\n" + "METHOD: " + log.Method + "\n" + "QUERY: " + log.Query + "\n" + "BODY: " + log.Body + "\n\n\n"; writer.Write(LogString); writer.Flush(); writer.Close(); } var serializer = new Newtonsoft.Json.JsonSerializer(); var stringWriter = new StringWriter(); using (var writer = new JsonTextWriter(stringWriter)) { writer.Formatting = Newtonsoft.Json.Formatting.Indented; writer.QuoteName = false; serializer.Serialize(writer, log); } var jsonString = stringWriter.ToString(); File.AppendAllText($"Log.json", jsonString); } if (_next != null) { await _next(httpContext); } }
public EnrollmentController(IStudentServiceDb service) { _service = service; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IStudentServiceDb service) { //chain of processing units that process our request (middlewares) if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } //swagger documentation app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Student Api V1"); }); //logging middleware // Since i have it at the top it will log all requests whether it is correct or not app.UseMiddleware <LogMiddleware>(); //MiddleWare to check student authorization if students wants to see his/her grades for example ( require index ) // note you would need to include 'grades' in path and Index as a key in header app.UseWhen(context => context.Request.Path.ToString().Contains("grades"), app => app.Use(async(context, next) => { if (!context.Request.Headers.ContainsKey("Index")) { //if index is not found => short circuit and return 401 context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Index number is required "); return; } //validate index by checking if index exists in the database String index = context.Request.Headers["Index"].ToString(); if (!service.CheckIndex(index)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Index number does not exist in the database"); return; } context.Response.StatusCode = StatusCodes.Status200OK; await context.Response.WriteAsync("Student with index number:" + index + " has the following grades: \n" + " bla bla bla get grades from database"); await next(); // call next middle ware })); //based on the url decide which endpoint should respond //eg: api/students StudentsController.getStudents app.UseRouting(); app.UseAuthorization(); /*we split routing and using the endpoints because we may want to use some * Authorization before actually calling the endpoint * calls the endpoints*/ app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public StudentsController(IStudentServiceDb service) { _service = service; }
public EnrollmentsController(IStudentServiceDb service, IConfiguration configuration, StudentContext context) { _service = service; _configuration = configuration; _context = context; }
public EnrollmentsController(IStudentServiceDb db) { _db = db; }
public EnrollmentsController(IStudentServiceDb service, IConfiguration configuration) { _service = service; _configuration = configuration; }
public EnrollmentController(IStudentServiceDb db) { _service = db; }
public EnrollmentsController(IStudentServiceDb service) { this.service = service; }