diff --git a/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs b/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs index aeb70cb0de..a258d0acaf 100644 --- a/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs +++ b/src/Microsoft.AspNetCore.Authentication/AuthenticationHandler.cs @@ -142,6 +142,10 @@ namespace Microsoft.AspNetCore.Authentication Logger.AuthenticationSchemeNotAuthenticated(Scheme.Name); } } + else if (result?.Failure != null) + { + Logger.AuthenticationSchemeNotAuthenticatedWithFailure(Scheme.Name, result.Failure.Message); + } return result; } diff --git a/src/Microsoft.AspNetCore.Authentication/LoggingExtensions.cs b/src/Microsoft.AspNetCore.Authentication/LoggingExtensions.cs index ed15e61904..46223d6471 100644 --- a/src/Microsoft.AspNetCore.Authentication/LoggingExtensions.cs +++ b/src/Microsoft.AspNetCore.Authentication/LoggingExtensions.cs @@ -9,6 +9,7 @@ namespace Microsoft.Extensions.Logging { private static Action _authSchemeAuthenticated; private static Action _authSchemeNotAuthenticated; + private static Action _authSchemeNotAuthenticatedWithFailure; private static Action _authSchemeChallenged; private static Action _authSchemeForbidden; private static Action _remoteAuthenticationError; @@ -32,6 +33,10 @@ namespace Microsoft.Extensions.Logging eventId: 6, logLevel: LogLevel.Debug, formatString: "The SigningIn event returned Skipped."); + _authSchemeNotAuthenticatedWithFailure = LoggerMessage.Define( + eventId: 7, + logLevel: LogLevel.Information, + formatString: "{AuthenticationScheme} was not authenticated. Failure message: {FailureMessage}"); _authSchemeAuthenticated = LoggerMessage.Define( eventId: 8, logLevel: LogLevel.Information, @@ -72,6 +77,11 @@ namespace Microsoft.Extensions.Logging _authSchemeNotAuthenticated(logger, authenticationScheme, null); } + public static void AuthenticationSchemeNotAuthenticatedWithFailure(this ILogger logger, string authenticationScheme, string failureMessage) + { + _authSchemeNotAuthenticatedWithFailure(logger, authenticationScheme, failureMessage, null); + } + public static void AuthenticationSchemeChallenged(this ILogger logger, string authenticationScheme) { _authSchemeChallenged(logger, authenticationScheme, null);