Skip to content

Avoid failed authN log when the endpoint has IAllowAnonymous metadata - #43212

Merged
Tratcher merged 2 commits into
dotnet:mainfrom
Kahbazi:kahbazi/NoLogIfAnonymous
Aug 15, 2022
Merged

Tratcher merged 2 commits into
dotnet:mainfrom
Kahbazi:kahbazi/NoLogIfAnonymous

Conversation

@Kahbazi

@Kahbazi Kahbazi commented Aug 11, 2022

Copy link
Copy Markdown
Member

Fixes #43042

@Kahbazi
Kahbazi requested a review from Tratcher as a code owner August 11, 2022 06:02
@ghost ghost added area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation community-contribution Indicates that the PR has been added by a community member labels Aug 11, 2022
@ghost

ghost commented Aug 11, 2022

Copy link
Copy Markdown

Thanks for your PR, @Kahbazi. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@Tratcher

Tratcher commented Aug 12, 2022 •

Copy link
Copy Markdown
Member

@Kahbazi I tracked down the reason for the original log and I think we can prevent it without this new anonymous check.

The following should be changed from Fail to NoResult.

return AuthenticateResult.Fail("Not authenticated");

Can you try that?

Background:
OIDC is being set as the default scheme, rather than cookies. The auth middleware then runs Authenticate on OIDC every request. That delegates to Cookies, which returns no identity. OIDC's base class then returns a failure instead of NoResult.

@Tratcher Tratcher self-assigned this Aug 12, 2022

@Tratcher Tratcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my prior comment.

@HaoK

HaoK commented Aug 12, 2022

Copy link
Copy Markdown
Member

So isn't this a subtle breaking change/behavior change? Users are able to check the authenticate result directly, and now we would be returning NoResult instead of Fail... Arguably there's no difference, but it still seems a bit risky

@halter73
halter73 self-requested a review August 13, 2022 00:55
@Tratcher

Copy link
Copy Markdown
Member

It is a minor behavior change, but I think it's a beneficial one. Auth didn't fail, there's just no user available. Also, I would not expect users to be directly saying Authenticate(OIDC), it would be Authenticate(Cookies) to get the current user and that doesn't go down this code path.

@davidfowl

Copy link
Copy Markdown
Member

I agree with @Tratcher on the fix, this code shouldn't be checking for allow anonymous.

@davidfowl davidfowl added the breaking-change This issue / pr will introduce a breaking change, when resolved / merged. label Aug 14, 2022
@davidfowl

Copy link
Copy Markdown
Member

I marked it as a breaking change. @Tratcher can you write up the breaking change after this PR gets merged?

@Tratcher
Tratcher merged commit ac4bd5e into dotnet:main Aug 15, 2022
@ghost ghost added this to the 7.0-rc1 milestone Aug 15, 2022
@Kahbazi
Kahbazi deleted the kahbazi/NoLogIfAnonymous branch August 16, 2022 05:05
@Tratcher

Copy link
Copy Markdown
Member

Done: aspnet/Announcements#491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-auth Includes: authentication, authorization, OAuth, OIDC, and access token validation breaking-change This issue / pr will introduce a breaking change, when resolved / merged. community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MapHealthChecks().AllowAnonymous() not working

6 participants