From 4b16fe5e5f2982832b54ba4523a6f0117571197c Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 19 May 2019 15:24:47 +0100 Subject: [PATCH] Use using dispose keyword 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. --- .../AmazonAuthenticationHandler.cs | 19 ++++--- .../ArcGISAuthenticationHandler.cs | 37 +++++++------ .../AsanaAuthenticationHandler.cs | 19 ++++--- .../AutodeskAuthenticationHandler.cs | 19 ++++--- .../AutomaticAuthenticationHandler.cs | 19 ++++--- .../BattleNetAuthenticationHandler.cs | 19 ++++--- .../BeamAuthenticationHandler.cs | 19 ++++--- .../BitbucketAuthenticationHandler.cs | 52 +++++++++---------- .../BufferAuthenticationHandler.cs | 19 ++++--- .../CiscoSparkAuthenticationHandler.cs | 19 ++++--- .../DeviantArtAuthenticationHandler.cs | 19 ++++--- .../DiscordAuthenticationHandler.cs | 19 ++++--- .../DropboxAuthenticationHandler.cs | 19 ++++--- .../EVEOnlineAuthenticationHandler.cs | 19 ++++--- .../FitbitAuthenticationHandler.cs | 23 ++++---- .../FoursquareAuthenticationHandler.cs | 31 ++++++----- .../GitHubAuthenticationHandler.cs | 52 +++++++++---------- .../GitterAuthenticationHandler.cs | 19 ++++--- .../HealthGraphAuthenticationHandler.cs | 19 ++++--- .../ImgurAuthenticationHandler.cs | 19 ++++--- .../InstagramAuthenticationHandler.cs | 19 ++++--- .../LinkedInAuthenticationHandler.cs | 19 ++++--- .../MailChimpAuthenticationHandler.cs | 19 ++++--- .../OnshapeAuthenticationHandler.cs | 19 ++++--- .../PatreonAuthenticationHandler.cs | 19 ++++--- .../PaypalAuthenticationHandler.cs | 19 ++++--- .../QQAuthenticationHandler.cs | 47 ++++++++--------- .../RedditAuthenticationHandler.cs | 23 ++++---- .../SalesforceAuthenticationHandler.cs | 19 ++++--- .../SlackAuthenticationHandler.cs | 19 ++++--- .../SoundCloudAuthenticationHandler.cs | 19 ++++--- .../SpotifyAuthenticationHandler.cs | 19 ++++--- .../StackExchangeAuthenticationHandler.cs | 23 ++++---- .../StravaAuthenticationHandler.cs | 19 ++++--- .../TwitchAuthenticationHandler.cs | 19 ++++--- .../UntappdAuthenticationHandler.cs | 19 ++++--- .../VimeoAuthenticationHandler.cs | 19 ++++--- .../VisualStudioAuthenticationHandler.cs | 23 ++++---- .../VkontakteAuthenticationHandler.cs | 27 ++++------ .../WeiboAuthenticationHandler.cs | 48 ++++++++--------- .../WeixinAuthenticationHandler.cs | 40 +++++++------- .../WordPressAuthenticationHandler.cs | 19 ++++--- .../YahooAuthenticationHandler.cs | 23 ++++---- .../YammerAuthenticationHandler.cs | 31 +++++------ .../YandexAuthenticationHandler.cs | 23 ++++---- 45 files changed, 508 insertions(+), 565 deletions(-) diff --git a/src/AspNet.Security.OAuth.Amazon/AmazonAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Amazon/AmazonAuthenticationHandler.cs index 26b65d104..1b853e0cc 100644 --- a/src/AspNet.Security.OAuth.Amazon/AmazonAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Amazon/AmazonAuthenticationHandler.cs @@ -51,22 +51,21 @@ public class AmazonAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class AsanaAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class AutodeskAuthenticationHandler : OAuthHandler 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 " + @@ -50,15 +50,14 @@ public class AutomaticAuthenticationHandler : OAuthHandler protected override async Task 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 " + @@ -48,15 +48,14 @@ public class BeamAuthenticationHandler : OAuthHandler 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); } } } diff --git a/src/AspNet.Security.OAuth.Bitbucket/BitbucketAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Bitbucket/BitbucketAuthenticationHandler.cs index 4d73780ae..7f5a4152a 100644 --- a/src/AspNet.Security.OAuth.Bitbucket/BitbucketAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Bitbucket/BitbucketAuthenticationHandler.cs @@ -33,11 +33,11 @@ public class BitbucketAuthenticationHandler : OAuthHandler 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 " + @@ -49,37 +49,36 @@ public class BitbucketAuthenticationHandler : OAuthHandler claim.Type == ClaimTypes.Email) && Options.Scope.Contains("email")) + // When the email address is not public, retrieve it from + // the emails endpoint if the user:email scope is specified. + if (!string.IsNullOrEmpty(Options.UserEmailsEndpoint) && + !identity.HasClaim(claim => claim.Type == ClaimTypes.Email) && Options.Scope.Contains("email")) + { + var address = await GetEmailAsync(tokens); + if (!string.IsNullOrEmpty(address)) { - var address = await GetEmailAsync(tokens); - if (!string.IsNullOrEmpty(address)) - { - identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer)); - } + identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer)); } - - 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); } protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse tokens) { - var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); // Failed requests shouldn't cause an error: in this case, return null to indicate that the email address cannot be retrieved. - 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.LogWarning("An error occurred while retrieving the email address associated with the logged in user: " + @@ -91,12 +90,11 @@ protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse return null; } - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) - { - return (from address in payload.RootElement.GetProperty("values").EnumerateArray() - where address.GetProperty("is_primary").GetBoolean() - select address.GetString("email")).FirstOrDefault(); - } + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + + return (from address in payload.RootElement.GetProperty("values").EnumerateArray() + where address.GetProperty("is_primary").GetBoolean() + select address.GetString("email")).FirstOrDefault(); } } } diff --git a/src/AspNet.Security.OAuth.Buffer/BufferAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Buffer/BufferAuthenticationHandler.cs index 3a5750a60..b670fdd26 100644 --- a/src/AspNet.Security.OAuth.Buffer/BufferAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Buffer/BufferAuthenticationHandler.cs @@ -32,11 +32,11 @@ public class BufferAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class BufferAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class DeviantArtAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class DiscordAuthenticationHandler : OAuthHandler CreateTicketAsync([NotNull] ClaimsIdentity identity, [NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens) { - var request = new HttpRequestMessage(HttpMethod.Post, Options.UserInformationEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, 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 " + @@ -48,15 +48,14 @@ public class DropboxAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class EVEOnlineAuthenticationHandler : OAuthHandler 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 " + @@ -51,22 +51,21 @@ public class FitbitAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}")); - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); @@ -77,7 +76,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["code"] = code }); - 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 an access token: the remote server " + diff --git a/src/AspNet.Security.OAuth.Foursquare/FoursquareAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Foursquare/FoursquareAuthenticationHandler.cs index 3735f6203..eb83ab1bd 100644 --- a/src/AspNet.Security.OAuth.Foursquare/FoursquareAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Foursquare/FoursquareAuthenticationHandler.cs @@ -42,9 +42,9 @@ public class FoursquareAuthenticationHandler : OAuthHandler 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 " + @@ -49,38 +49,37 @@ public class GitHubAuthenticationHandler : OAuthHandler claim.Type == ClaimTypes.Email) && Options.Scope.Contains("user:email")) + // When the email address is not public, retrieve it from + // the emails endpoint if the user:email scope is specified. + if (!string.IsNullOrEmpty(Options.UserEmailsEndpoint) && + !identity.HasClaim(claim => claim.Type == ClaimTypes.Email) && Options.Scope.Contains("user:email")) + { + var address = await GetEmailAsync(tokens); + if (!string.IsNullOrEmpty(address)) { - var address = await GetEmailAsync(tokens); - if (!string.IsNullOrEmpty(address)) - { - identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer)); - } + identity.AddClaim(new Claim(ClaimTypes.Email, address, ClaimValueTypes.String, Options.ClaimsIssuer)); } - - 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); } protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse tokens) { // See https://developer.github.com/v3/users/emails/ for more information about the /user/emails endpoint. - var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserEmailsEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); // Failed requests shouldn't cause an error: in this case, return null to indicate that the email address cannot be retrieved. - 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.LogWarning("An error occurred while retrieving the email address associated with the logged in user: " + @@ -92,12 +91,11 @@ protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse return null; } - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) - { - return (from address in payload.RootElement.EnumerateArray() - where address.GetProperty("primary").GetBoolean() - select address.GetString("email")).FirstOrDefault(); - } + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + + return (from address in payload.RootElement.EnumerateArray() + where address.GetProperty("primary").GetBoolean() + select address.GetString("email")).FirstOrDefault(); } } } diff --git a/src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHandler.cs index 1bf28c400..62f587eed 100644 --- a/src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHandler.cs @@ -32,11 +32,11 @@ public class GitterAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class GitterAuthenticationHandler : OAuthHandler 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/vnd.com.runkeeper.User+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 " + @@ -48,15 +48,14 @@ public class HealthGraphAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class ImgurAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class MailChimpAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class OnshapeAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class PatreonAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class PaypalAuthenticationHandler : OAuthHandler ["openid"] = identifier, }); - var response = await Backchannel.GetAsync(address); + using var response = await Backchannel.GetAsync(address); if (!response.IsSuccessStatusCode) { Logger.LogError("An error occurred while retrieving the user profile: the remote server " + @@ -65,26 +65,25 @@ public class QQAuthenticationHandler : OAuthHandler throw new HttpRequestException("An error occurred while retrieving user information."); } - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + + var status = payload.RootElement.GetProperty("ret").GetInt32(); + if (status != 0) { - var status = payload.RootElement.GetProperty("ret").GetInt32(); - if (status != 0) - { - Logger.LogError("An error occurred while retrieving the user profile: the remote server " + - "returned a {Status} response with the following message: {Message}.", - /* Status: */ status, - /* Message: */ payload.RootElement.GetString("msg")); + Logger.LogError("An error occurred while retrieving the user profile: the remote server " + + "returned a {Status} response with the following message: {Message}.", + /* Status: */ status, + /* Message: */ payload.RootElement.GetString("msg")); - throw new HttpRequestException("An error occurred while retrieving user information."); - } + throw new HttpRequestException("An error occurred while retrieving user information."); + } - 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); } protected override async Task ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) @@ -98,9 +97,9 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["grant_type"] = "authorization_code", }); - var request = new HttpRequestMessage(HttpMethod.Get, address); + using var request = new HttpRequestMessage(HttpMethod.Get, address); - 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 an access token: the remote server " + @@ -121,9 +120,9 @@ protected override async Task ExchangeCodeAsync([NotNull] st private async Task GetUserIdentifierAsync(OAuthTokenResponse tokens) { var address = QueryHelpers.AddQueryString(Options.UserIdentificationEndpoint, "access_token", tokens.AccessToken); - var request = new HttpRequestMessage(HttpMethod.Get, address); + using var request = new HttpRequestMessage(HttpMethod.Get, address); - 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 identifier: the remote server " + @@ -143,10 +142,8 @@ private async Task GetUserIdentifierAsync(OAuthTokenResponse tokens) body = body.Substring(index, body.LastIndexOf("}") - index + 1); } - using (var payload = JsonDocument.Parse(body)) - { - return payload.RootElement.GetString("openid"); - } + using var payload = JsonDocument.Parse(body); + return payload.RootElement.GetString("openid"); } protected override string FormatScope() => string.Join(",", Options.Scope); diff --git a/src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs index 5791cb066..681866dbf 100644 --- a/src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Reddit/RedditAuthenticationHandler.cs @@ -36,7 +36,7 @@ public class RedditAuthenticationHandler : OAuthHandler 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); @@ -47,7 +47,7 @@ public class RedditAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] st { var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}")); - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); @@ -109,7 +108,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["code"] = code }); - 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 an access token: the remote server " + diff --git a/src/AspNet.Security.OAuth.Salesforce/SalesforceAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Salesforce/SalesforceAuthenticationHandler.cs index a11101602..3968c6eb7 100644 --- a/src/AspNet.Security.OAuth.Salesforce/SalesforceAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Salesforce/SalesforceAuthenticationHandler.cs @@ -34,12 +34,12 @@ public class SalesforceAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class SpotifyAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint) + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint) { Content = new FormUrlEncodedContent(new Dictionary { @@ -96,7 +95,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st }) }; - 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 an access token: the remote server " + diff --git a/src/AspNet.Security.OAuth.Strava/StravaAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Strava/StravaAuthenticationHandler.cs index a3421c8aa..7660e1e89 100644 --- a/src/AspNet.Security.OAuth.Strava/StravaAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Strava/StravaAuthenticationHandler.cs @@ -35,11 +35,11 @@ public class StravaAuthenticationHandler : OAuthHandler 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 " + @@ -51,15 +51,14 @@ public class StravaAuthenticationHandler : OAuthHandler string.Join(",", Options.Scope); diff --git a/src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationHandler.cs index 5ee45c94e..ef169c93a 100644 --- a/src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Twitch/TwitchAuthenticationHandler.cs @@ -45,11 +45,11 @@ protected override string BuildChallengeUrl(AuthenticationProperties properties, protected override async Task 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 " + @@ -61,15 +61,14 @@ protected override string BuildChallengeUrl(AuthenticationProperties properties, 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); } } } diff --git a/src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHandler.cs index 844dfa716..dbc3444c9 100644 --- a/src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHandler.cs @@ -32,11 +32,11 @@ public class UntappdAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class UntappdAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class VimeoAuthenticationHandler : OAuthHandler 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 " + @@ -51,20 +51,19 @@ public class VisualStudioAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded")); request.Content = new FormUrlEncodedContent(new Dictionary @@ -76,7 +75,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["client_assertion_type"] = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" }); - var response = await Backchannel.SendAsync(request, Context.RequestAborted); + using var response = await Backchannel.SendAsync(request, Context.RequestAborted); if (!response.IsSuccessStatusCode) { diff --git a/src/AspNet.Security.OAuth.Vkontakte/VkontakteAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Vkontakte/VkontakteAuthenticationHandler.cs index f5f39a99c..d2fd34ed3 100644 --- a/src/AspNet.Security.OAuth.Vkontakte/VkontakteAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Vkontakte/VkontakteAuthenticationHandler.cs @@ -45,7 +45,7 @@ public class VkontakteAuthenticationHandler : OAuthHandler claim.Type == ClaimTypes.Email) && Options.Scope.Contains("email")) { - // When the email address is not public, retrieve it from - // the emails endpoint if the user:email scope is specified. - if (!string.IsNullOrEmpty(Options.UserEmailsEndpoint) && - !identity.HasClaim(claim => claim.Type == ClaimTypes.Email) && Options.Scope.Contains("email")) + var email = await GetEmailAsync(tokens); + if (!string.IsNullOrEmpty(address)) { - var email = await GetEmailAsync(tokens); - if (!string.IsNullOrEmpty(address)) - { - identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer)); - } + identity.AddClaim(new Claim(ClaimTypes.Email, email, ClaimValueTypes.String, Options.ClaimsIssuer)); } + } - 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); } protected override string FormatScope() => string.Join(",", Options.Scope); @@ -89,11 +88,11 @@ protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse ["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")); // Failed requests shouldn't cause an error: in this case, return null to indicate that the email address cannot be retrieved. - 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.LogWarning("An error occurred while retrieving the email address associated with the logged in user: " + @@ -105,11 +104,10 @@ protected virtual async Task GetEmailAsync([NotNull] OAuthTokenResponse return null; } - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) - { - return (from email in payload.RootElement.EnumerateArray() - select email.GetString("email")).FirstOrDefault(); - } + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + + return (from email in payload.RootElement.EnumerateArray() + select email.GetString("email")).FirstOrDefault(); } } } diff --git a/src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs index 62fdbe82c..1ef422d97 100644 --- a/src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Weixin/WeixinAuthenticationHandler.cs @@ -39,7 +39,7 @@ protected override async Task CreateTicketAsync([NotNull] ["openid"] = tokens.Response.RootElement.GetString("openid") }); - var response = await Backchannel.GetAsync(address); + using var response = await Backchannel.GetAsync(address); if (!response.IsSuccessStatusCode) { Logger.LogError("An error occurred while retrieving the user profile: the remote server " + @@ -51,26 +51,25 @@ protected override async Task CreateTicketAsync([NotNull] throw new HttpRequestException("An error occurred while retrieving user information."); } - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + + if (!string.IsNullOrEmpty(payload.RootElement.GetString("errcode"))) { - if (!string.IsNullOrEmpty(payload.RootElement.GetString("errcode"))) - { - Logger.LogError("An error occurred while retrieving the user profile: the remote server " + - "returned a {Status} response with the following payload: {Headers} {Body}.", - /* Status: */ response.StatusCode, - /* Headers: */ response.Headers.ToString(), - /* Body: */ await response.Content.ReadAsStringAsync()); - - throw new HttpRequestException("An error occurred while retrieving user information."); - } - - 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); + Logger.LogError("An error occurred while retrieving the user profile: the remote server " + + "returned a {Status} response with the following payload: {Headers} {Body}.", + /* Status: */ response.StatusCode, + /* Headers: */ response.Headers.ToString(), + /* Body: */ await response.Content.ReadAsStringAsync()); + + throw new HttpRequestException("An error occurred while retrieving user information."); } + + 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 async Task ExchangeCodeAsync(string code, string redirectUri) @@ -83,7 +82,7 @@ protected override async Task ExchangeCodeAsync(string code, ["grant_type"] = "authorization_code" }); - var response = await Backchannel.GetAsync(address); + using var response = await Backchannel.GetAsync(address); if (!response.IsSuccessStatusCode) { Logger.LogError("An error occurred while retrieving an access token: the remote server " + @@ -106,6 +105,7 @@ protected override async Task ExchangeCodeAsync(string code, return OAuthTokenResponse.Failed(new Exception("An error occurred while retrieving an access token.")); } + return OAuthTokenResponse.Success(payload); } diff --git a/src/AspNet.Security.OAuth.WordPress/WordPressAuthenticationHandler.cs b/src/AspNet.Security.OAuth.WordPress/WordPressAuthenticationHandler.cs index 3247af927..3f7f5e957 100644 --- a/src/AspNet.Security.OAuth.WordPress/WordPressAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.WordPress/WordPressAuthenticationHandler.cs @@ -32,11 +32,11 @@ public class WordPressAuthenticationHandler : OAuthHandler 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 " + @@ -48,15 +48,14 @@ public class WordPressAuthenticationHandler : OAuthHandler 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 " + @@ -51,22 +51,21 @@ public class YahooAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}")); - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); @@ -77,7 +76,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["code"] = code }); - 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 an access token: the remote server " + diff --git a/src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs index 65aa4d7d8..dd9a3dcb8 100644 --- a/src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs @@ -35,10 +35,10 @@ public class YammerAuthenticationHandler : OAuthHandler 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.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 " + @@ -50,21 +50,20 @@ public class YammerAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Content = new FormUrlEncodedContent(new Dictionary @@ -76,7 +75,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["grant_type"] = "authorization_code" }); - 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 an access token: the remote server " + @@ -91,12 +90,8 @@ protected override async Task ExchangeCodeAsync([NotNull] st // Note: Yammer doesn't return a standard OAuth2 response. To make this middleware compatible // with the OAuth2 generic middleware, a compliant JSON payload is generated manually. // See https://developer.yammer.com/docs/oauth-2 for more information about this process. - string accessToken; - - using (var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync())) - { - accessToken = payload.RootElement.GetProperty("access_token").GetString("token"); - } + using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); + string accessToken = payload.RootElement.GetProperty("access_token").GetString("token"); var token = await CreateAccessTokenAsync(accessToken); return OAuthTokenResponse.Success(token); diff --git a/src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs b/src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs index 3bd30b902..07c906882 100644 --- a/src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs +++ b/src/AspNet.Security.OAuth.Yandex/YandexAuthenticationHandler.cs @@ -35,11 +35,11 @@ public class YandexAuthenticationHandler : OAuthHandler 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 " + @@ -51,22 +51,21 @@ public class YandexAuthenticationHandler : OAuthHandler ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Options.ClientId}:{Options.ClientSecret}")); - var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); + using var request = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials); @@ -77,7 +76,7 @@ protected override async Task ExchangeCodeAsync([NotNull] st ["code"] = code }); - 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 an access token: the remote server " +