public class LoggingMiddleware { private readonly AppFunc _next; public LoggingMiddleware(AppFunc next) { _next = next; } public async Task Invoke(IDictionaryenvironment) { var request = new OwinRequest(environment); var response = new OwinResponse(environment); // Log request information Console.WriteLine($"Request: {request.Method} {request.Uri}"); // Call the next middleware in the pipeline await _next(environment); // Log response information Console.WriteLine($"Response: {response.StatusCode}"); } }
public class HttpsRedirectMiddleware { private readonly AppFunc _next; public HttpsRedirectMiddleware(AppFunc next) { _next = next; } public async Task Invoke(IDictionaryThis middleware checks if the request is not using HTTPS, and if so, generates a new HTTPS URL using the UriBuilder class and sets the status code to 301 (permanent redirect). It then sets the Location header to the new URL and does not call the next middleware. If the request is already using HTTPS, it calls the next middleware. The package library for AppFunc is Microsoft.Owin.environment) { var request = new OwinRequest(environment); if (!request.Uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { var uriBuilder = new UriBuilder(request.Uri) { Scheme = "https", Port = 443 }; var response = new OwinResponse(environment) { StatusCode = 301 }; response.Headers["Location"] = uriBuilder.Uri.ToString(); } else { await _next(environment); } } }