diff --git a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs index 675ae51494..0df42597dd 100644 --- a/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication.Facebook/FacebookHandler.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Globalization; using System.Net.Http; using System.Security.Claims; @@ -35,7 +36,11 @@ namespace Microsoft.AspNetCore.Authentication.Facebook } var response = await Backchannel.GetAsync(endpoint, Context.RequestAborted); - response.EnsureSuccessStatusCode(); + if (!response.IsSuccessStatusCode) + { + var errorMessage = $"Failed to retrived Facebook user information ({response.StatusCode}) Please check if the authentication information is correct and the corresponding Google API is enabled."; + throw new InvalidOperationException(errorMessage); + } var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); @@ -119,7 +124,7 @@ namespace Microsoft.AspNetCore.Authentication.Facebook { identity.AddClaim(new Claim(ClaimTypes.Name, name, ClaimValueTypes.String, Options.ClaimsIssuer)); } - + var timeZone = FacebookHelper.GetTimeZone(payload); if (!string.IsNullOrEmpty(timeZone)) { diff --git a/src/Microsoft.AspNetCore.Authentication.Google/GoogleHandler.cs b/src/Microsoft.AspNetCore.Authentication.Google/GoogleHandler.cs index 6e3ee36939..4a81744b05 100644 --- a/src/Microsoft.AspNetCore.Authentication.Google/GoogleHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication.Google/GoogleHandler.cs @@ -32,7 +32,11 @@ namespace Microsoft.AspNetCore.Authentication.Google request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); var response = await Backchannel.SendAsync(request, Context.RequestAborted); - response.EnsureSuccessStatusCode(); + if (!response.IsSuccessStatusCode) + { + var errorMessage = $"Failed to retrived Google user information ({response.StatusCode}) Please check if the authentication information is correct and the corresponding Google API is enabled."; + throw new InvalidOperationException(errorMessage); + } var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); diff --git a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountHandler.cs b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountHandler.cs index 8b9177625c..18f5df9d4a 100644 --- a/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication.MicrosoftAccount/MicrosoftAccountHandler.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Net.Http; using System.Net.Http.Headers; using System.Security.Claims; @@ -25,7 +26,11 @@ namespace Microsoft.AspNetCore.Authentication.MicrosoftAccount request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); var response = await Backchannel.SendAsync(request, Context.RequestAborted); - response.EnsureSuccessStatusCode(); + if (!response.IsSuccessStatusCode) + { + var errorMessage = $"Failed to retrived Microsoft user information ({response.StatusCode}) Please check if the authentication information is correct and the corresponding Google API is enabled."; + throw new InvalidOperationException(errorMessage); + } var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); diff --git a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthHandler.cs b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthHandler.cs index 12b85ae7e6..29d46367db 100644 --- a/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication.OAuth/OAuthHandler.cs @@ -119,7 +119,15 @@ namespace Microsoft.AspNetCore.Authentication.OAuth properties.StoreTokens(authTokens); } - return AuthenticateResult.Success(await CreateTicketAsync(identity, properties, tokens)); + try + { + var ticket = await CreateTicketAsync(identity, properties, tokens); + return AuthenticateResult.Success(ticket); + } + catch (Exception ex) + { + return AuthenticateResult.Fail(ex); + } } protected virtual async Task ExchangeCodeAsync(string code, string redirectUri)