Skip to content

Use newer method overloads in auth handlers #30715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
8 commits merged into from
Mar 7, 2021

Conversation

martincostello
Copy link
Member

@martincostello martincostello commented Mar 6, 2021

PR Title

Use newly added overloads of already-used methods in authentication handlers.

PR Description

I noticed that some of the new overloads added in .NET 5.0 weren't used, so I've switched various usages within the Authentication part of the solution to use them. I also applies some minor improvements in the files I touched that Visual Studio suggested.

  • Use new overloads of methods added in .NET 5.0 that accept a CancellationToken.
  • Use WriteAsync() overload that accepts a span and a CancellationToken, instead of an array and indexes.
  • Use StringBuilder.Append(char) instead of StringBuilder.Append(string) when there is only a single character.
  • Make some methods that don't access instance data static.
  • Remove some unused code.
  • Use compound assignment.
  • Fix two incorrect test names from copy-paste.

Use new overloads of methods added in .NET 5.0 that accept a CancellationToken.
Use StringBuilder.Append(char) instead of StringBuilder.Append(string) when there is only a single character.
Use WriteAsync() overload that accepts a span and a CancellationToken, instead of an array and indexes to write all bytes.
Make methods that do not access instance data static.
Remove an unused method and an unused parameter.
Use the compound assignment operator.
Fix copy-paste from Facebook tests.
@martincostello martincostello requested a review from Tratcher as a code owner March 6, 2021 10:11
@ghost ghost added the area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer label Mar 6, 2021
@@ -276,7 +276,7 @@ public async virtual Task SignOutAsync(AuthenticationProperties properties)
Response.Headers[HeaderNames.Pragma] = "no-cache";
Response.Headers[HeaderNames.Expires] = HeaderValueEpocDate;

await Response.Body.WriteAsync(buffer, 0, buffer.Length);
await Response.Body.WriteAsync(buffer, Context.RequestAborted);
Copy link
Member

Choose a reason for hiding this comment

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

It's better not to pass the CT to WriteAsync. The write will fail silently if the client disconnects, but if you pass the CT then it will throw. We don't care if this write succeeds or fails.

@Tratcher Tratcher self-assigned this Mar 6, 2021
Remove the CancellationToken from the WriteAsync() call to address review feedback.
@Tratcher Tratcher added this to the 6.0-preview3 milestone Mar 7, 2021
@ghost
Copy link

ghost commented Mar 7, 2021

Hello @Tratcher!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit a24aed7 into dotnet:main Mar 7, 2021
@martincostello martincostello deleted the Use-CancellationToken-More branch March 7, 2021 07:19
@Tratcher
Copy link
Member

Tratcher commented Mar 7, 2021

Thanks

@@ -105,7 +105,7 @@ protected virtual async Task<bool> HandleRemoteSignOutAsync()
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
&& Request.Body.CanRead)
{
var form = await Request.ReadFormAsync();
var form = await Request.ReadFormAsync(Context.RequestAborted);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this? This happens automagically without passing in the token.

Copy link
Member Author

Choose a reason for hiding this comment

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

TIL - I didn't realise this was equivalent to the default behaviour with CancellationToken.None/default.

@@ -148,7 +148,7 @@ protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
&& Request.Body.CanRead)
{
var form = await Request.ReadFormAsync();
var form = await Request.ReadFormAsync(Context.RequestAborted);
Copy link
Member

Choose a reason for hiding this comment

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

This too.

@davidfowl
Copy link
Member

In general we shouldn't need to pass the request aborted token reading the request. The server implementation already has knowledge of the so we don't need to pay the cost here.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants