using System; using System.Text; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; namespace TradeIdeasCommon.Swagger { public class SwaggerBasicAuthMiddleware { private readonly RequestDelegate next; public SwaggerBasicAuthMiddleware(RequestDelegate next) { this.next = next; } public async Task InvokeAsync(HttpContext context) { if (context.Request.Path.ToString().Contains("/swagger") && !this.IsLocalRequest(context)) //if(context.Request.Path.StartsWithSegments("/TradeIdeasWebService/swagger")) { string authHeader = context.Request.Headers["Authorization"]; if (authHeader != null && authHeader.StartsWith("Basic ")) { // Get the encoded username and password var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim(); // Decode from Base64 to string var decodedUsernamePassword = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword)); // Split username and password var username = decodedUsernamePassword.Split(':', 2)[0]; var password = decodedUsernamePassword.Split(':', 2)[1]; // Check if login is correct if (IsAuthorized(username, password)) { await next.Invoke(context); return; } } // Return authentication type (causes browser to show login dialog) context.Response.Headers["WWW-Authenticate"] = "Basic"; // Return unauthorized context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } else { await next.Invoke(context); } } public bool IsAuthorized(string username, string password) { // Check that username and password are correct return username.Equals("BrokerWebServices", StringComparison.InvariantCultureIgnoreCase) && password.Equals("Poje0H3XHW"); } public bool IsLocalRequest(HttpContext context) { if (context.Connection.RemoteIpAddress == null && context.Connection.LocalIpAddress == null) { return true; } if (context.Connection.RemoteIpAddress.Equals(context.Connection.LocalIpAddress)) { return true; } if (IPAddress.IsLoopback(context.Connection.RemoteIpAddress)) { return true; } return false; } } }