Skip to content

Commit

Permalink
Use using dispose keyword
Browse files Browse the repository at this point in the history
Use the new C# 8.0 "using var ..." syntax to dipose of relevant resources without needing to use using blocks in the handlers to reduce the diff with the 2.x versions.
  • Loading branch information
martincostello committed May 19, 2019
1 parent 5ebdfe7 commit 4b16fe5
Show file tree
Hide file tree
Showing 45 changed files with 508 additions and 565 deletions.
19 changes: 9 additions & 10 deletions src/AspNet.Security.OAuth.Amazon/AmazonAuthenticationHandler.cs
Expand Up @@ -51,22 +51,21 @@ public class AmazonAuthenticationHandler : OAuthHandler<AmazonAuthenticationOpti
endpoint = QueryHelpers.AddQueryString(endpoint, "fields", string.Join(",", Options.Fields));
}

var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
using var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
response.EnsureSuccessStatusCode();

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
37 changes: 18 additions & 19 deletions src/AspNet.Security.OAuth.ArcGIS/ArcGISAuthenticationHandler.cs
Expand Up @@ -42,33 +42,32 @@ public class ArcGISAuthenticationHandler : OAuthHandler<ArcGISAuthenticationOpti
["token"] = tokens.AccessToken
});

var request = new HttpRequestMessage(HttpMethod.Get, address);
using var request = new HttpRequestMessage(HttpMethod.Get, address);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// Request the token
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

// Note: error responses always return 200 status codes.
if (payload.RootElement.TryGetProperty("error", out var error))
{
// Note: error responses always return 200 status codes.
if (payload.RootElement.TryGetProperty("error", out var error))
{
// See https://developers.arcgis.com/authentication/server-based-user-logins/ for more information
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
"returned a response with the following error code: {Code} {Message}.",
/* Code: */ error.GetString("code"),
/* Message: */ error.GetString("message"));
// See https://developers.arcgis.com/authentication/server-based-user-logins/ for more information
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
"returned a response with the following error code: {Code} {Message}.",
/* Code: */ error.GetString("code"),
/* Message: */ error.GetString("message"));

throw new InvalidOperationException("An error occurred while retrieving the user profile.");
}
throw new InvalidOperationException("An error occurred while retrieving the user profile.");
}

var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
19 changes: 9 additions & 10 deletions src/AspNet.Security.OAuth.Asana/AsanaAuthenticationHandler.cs
Expand Up @@ -32,11 +32,11 @@ public class AsanaAuthenticationHandler : OAuthHandler<AsanaAuthenticationOption
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
Expand All @@ -48,15 +48,14 @@ public class AsanaAuthenticationHandler : OAuthHandler<AsanaAuthenticationOption
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions(payload.RootElement.GetProperty("data"));
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions(payload.RootElement.GetProperty("data"));

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
Expand Up @@ -32,11 +32,11 @@ public class AutodeskAuthenticationHandler : OAuthHandler<AutodeskAuthentication
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
Expand All @@ -48,15 +48,14 @@ public class AutodeskAuthenticationHandler : OAuthHandler<AutodeskAuthentication
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
Expand Up @@ -34,11 +34,11 @@ public class AutomaticAuthenticationHandler : OAuthHandler<AutomaticAuthenticati
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
Expand All @@ -50,15 +50,14 @@ public class AutomaticAuthenticationHandler : OAuthHandler<AutomaticAuthenticati
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}

protected override string BuildChallengeUrl(AuthenticationProperties properties, string redirectUri)
Expand Down
Expand Up @@ -35,10 +35,10 @@ public class BattleNetAuthenticationHandler : OAuthHandler<BattleNetAuthenticati
{
var address = QueryHelpers.AddQueryString(Options.UserInformationEndpoint, "access_token", tokens.AccessToken);

var request = new HttpRequestMessage(HttpMethod.Get, address);
using var request = new HttpRequestMessage(HttpMethod.Get, address);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
Expand All @@ -50,15 +50,14 @@ public class BattleNetAuthenticationHandler : OAuthHandler<BattleNetAuthenticati
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
19 changes: 9 additions & 10 deletions src/AspNet.Security.OAuth.Beam/BeamAuthenticationHandler.cs
Expand Up @@ -32,11 +32,11 @@ public class BeamAuthenticationHandler : OAuthHandler<BeamAuthenticationOptions>
protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity,
[NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens)
{
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);

var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
if (!response.IsSuccessStatusCode)
{
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
Expand All @@ -48,15 +48,14 @@ public class BeamAuthenticationHandler : OAuthHandler<BeamAuthenticationOptions>
throw new HttpRequestException("An error occurred while retrieving the user profile.");
}

using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()))
{
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
var principal = new ClaimsPrincipal(identity);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
context.RunClaimActions();

await Options.Events.CreatingTicket(context);
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}

0 comments on commit 4b16fe5

Please sign in to comment.