我有一个ASP.NET Core 2.1 Web应用程序项目,它使用JWT令牌对项目内置的Web API进行身份验证。当我在我的机器上本地运行它时,它工作得很好,但是当我将它部署到Azure(使用相同的环境和应用程序设置)时,它只会返回空的http401响应,以响应来自经过身份验证的客户端的请求,我需要找出原因以便修复它。
我在ASP.NET内核中启用了对每个细节的日志记录,但是我从未收到任何有用的输出。
首先,我补充道
Serilog.AspNetCore
控制台通过NuGet接收到项目,然后在
Verbose
水平
Program.cs
:
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Verbose)
.MinimumLevel.Override("System", LogEventLevel.Verbose)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Verbose)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Literate)
.CreateLogger();
CreateWebHostBuilder( args ).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(String[] args)
{
return WebHost.CreateDefaultBuilder( args )
.ConfigureLogging( (ctx, cfg ) =>
{
cfg.ClearProviders();
} )
.UseStartup<Startup>()
.UseSerilog();
}
}
stdout
登录到文件)我得到以下输出:
[04:13:10 Verbose] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker
Authorization Filter: Before executing OnAuthorizationAsync on filter
Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter.
[04:13:10 Verbose]
IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler
HandleAuthenticateAsync called
[04:13:10 Debug]
IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler
AuthenticationScheme: Bearer was not authenticated.
[04:13:10 Information]
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService
Authorization failed.
[04:13:10 Verbose] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker
Authorization Filter: After executing OnAuthorizationAsync on filter
Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter.
[04:13:10 Information]
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker
Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
[04:13:10 Verbose] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker
Before executing action result Microsoft.AspNetCore.Mvc.ChallengeResult.
[04:13:10 Information] Microsoft.AspNetCore.Mvc.ChallengeResult
Executing ChallengeResult with authentication schemes (["Bearer"]).
[04:13:10 Verbose]
IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler
Forwarding challenge to scheme: BearerIdentityServerAuthenticationJwt
请注意,尽管日志记录很详细,但错误消息(下面重复)并没有给出任何解释:
AuthenticationScheme: Bearer was not authenticated.
Authorization failed.
我翻遍了ASP.NET核心安全源代码来看看
JwtBearerHandler.HandleAuthenticateAsync
System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler
它通常会进行大量的日志记录,包括详细的原因(例如
IDX10209
-在字符串中键入错误代码),但我不知道为什么它不输出任何我可以捕获的内容。
如何记录来自的消息
JwtSecurityTokenHandler
?