Implementation for resolve issue #993#996
Merged
thiagoloureiro merged 1 commit intoThreeMammals:developfrom Sep 23, 2019
Merged
Implementation for resolve issue #993#996thiagoloureiro merged 1 commit intoThreeMammals:developfrom
thiagoloureiro merged 1 commit intoThreeMammals:developfrom
Conversation
Contributor
|
Hello, we are having a similar problems with the OPTIONS request. By default angular seems to add the OPTIONS before any other request, and since they are failing because of the authentication middleware it fails to do the final request. OPTIONS should not be handled by the authorization middleware. Edit: but we should take care, that if the we invoke the next after by-passing the authorization middleware and we have configured another middleware like ClaimsToHeaders, etc. it will fail in that middleware. |
Contributor
Author
|
Ok, based me in the Authentication class implementation, that follow same solution public class AuthenticationMiddleware : OcelotMiddleware
{
private readonly OcelotRequestDelegate _next;
public AuthenticationMiddleware(OcelotRequestDelegate next,
IOcelotLoggerFactory loggerFactory)
: base(loggerFactory.CreateLogger<AuthenticationMiddleware>())
{
_next = next;
}
public async Task Invoke(DownstreamContext context)
{
if (context.HttpContext.Request.Method.ToUpper() != "OPTIONS" && IsAuthenticatedRoute(context.DownstreamReRoute))
{
Logger.LogInformation($"{context.HttpContext.Request.Path} is an authenticated route. {MiddlewareName} checking if client is authenticated");
var result = await context.HttpContext.AuthenticateAsync(context.DownstreamReRoute.AuthenticationOptions.AuthenticationProviderKey);
context.HttpContext.User = result.Principal;
if (context.HttpContext.User.Identity.IsAuthenticated)
{
Logger.LogInformation($"Client has been authenticated for {context.HttpContext.Request.Path}");
await _next.Invoke(context);
}
else
{
var error = new UnauthenticatedError(
$"Request for authenticated route {context.HttpContext.Request.Path} by {context.HttpContext.User.Identity.Name} was unauthenticated");
Logger.LogWarning($"Client has NOT been authenticated for {context.HttpContext.Request.Path} and pipeline error set. {error}");
SetPipelineError(context, error);
}
}
else
{
Logger.LogInformation($"No authentication needed for {context.HttpContext.Request.Path}");
await _next.Invoke(context);
}
}
private static bool IsAuthenticatedRoute(DownstreamReRoute reRoute)
{
return reRoute.IsAuthenticated;
}
}Thank's |
This was referenced Mar 16, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes / New Feature #
Proposed Changes