CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2024-10-29
by CoffeeCode

Original Post

Original - Posted on 2021-06-26
by Darshani Jayasekara



            
Present in both answers; Present only in the new answer; Present only in the old answer;

If you are trying to clear cache and headers after closing browser.
Here is what you can add:
public async Task InvokeAsync(HttpContext context) { //Make sure we are hitting the swagger path, and not doing it locally as it just gets annoying :-) if (context.Request.Path.StartsWithSegments("/swagger") && !this.IsLocalRequest(context)) { 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];
//remove header from cache context.Request.Headers.Remove("Authorization"); context.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue() { Public = false, MaxAge = TimeSpan.FromSeconds(0) };
// 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); } }

I was able to enhance @Ricky G's answer to support asp.net core identity authentication mechanism.
In SwaggerAuthenticationMiddleware ,
```cs public async Task InvokeAsync(HttpContext context) { //Make sure we are hitting the swagger path, and not doing it locally as it just gets annoying :-) if (context.Request.Path.StartsWithSegments("/swagger")) { if (!context.User.Identity.IsAuthenticated) { 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];
var signInManager = _httpContextAccessor.HttpContext.RequestServices.GetService<SignInManager<IdpUser>>(); var result = await signInManager.PasswordSignInAsync(username, password, false, lockoutOnFailure: false); if (result.Succeeded) { 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); return; } } else { await next.Invoke(context); } } ```
In Startup.cs you have to register HttpContextAccessor like below.
```cs services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;