Skip to content

Commit 4a59d5c

Browse files
Dispose of resources
Dispose of HttpRequestMessage and HttpResponseMessage. Simplify JSON parsing by removing using block.
1 parent 5f769b9 commit 4a59d5c

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

generators/app/templates/AuthenticationHandler.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
5151
// TODO Append any additional query string parameters required
5252
//endpoint = QueryHelpers.AddQueryString(endpoint, "token", tokens.AccessToken);
5353

54-
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
54+
using var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
5555
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
5656

5757
// TODO Add any HTTP request headers required
5858
//request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
5959

60-
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
60+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
6161
if (!response.IsSuccessStatusCode)
6262
{
6363
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
@@ -69,15 +69,14 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync(
6969
throw new HttpRequestException("An error occurred while retrieving the user profile from <%= name %>.");
7070
}
7171

72-
using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
73-
{
74-
var principal = new ClaimsPrincipal(identity);
75-
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
76-
context.RunClaimActions();
72+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
7773

78-
await Options.Events.CreatingTicket(context);
79-
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
80-
}
74+
var principal = new ClaimsPrincipal(identity);
75+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
76+
context.RunClaimActions();
77+
78+
await Options.Events.CreatingTicket(context);
79+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
8180
}
8281
}
8382
}

0 commit comments

Comments
 (0)