【发布时间】:2018-10-02 04:22:37
【问题描述】:
我有一个 ASP.NET Core 2.1 Web 应用程序项目,它使用 JWT 令牌对项目内置的 Web API 进行身份验证。当我在我的机器上本地运行它时它工作正常,但是当我将它部署到 Azure(具有相同的环境和应用程序设置)时,它只是返回空的 HTTP 401 响应来自我经过身份验证的客户端的请求,我需要找出原因所以我可以修。
我在 ASP.NET Core 中启用了每个细节的日志记录,但是我从未收到任何有用的输出。
首先,我通过 NuGet 将Serilog.AspNetCore 和 Console sink 添加到项目中,然后在Program.cs 中配置Verbose 级别的日志记录:
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();
}
}
但是当我在 Azure 上运行我的 Web 应用程序(使用控制台 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 Core Security 源代码,发现 JwtBearerHandler.HandleAuthenticateAsync 本身并没有做太多的日志记录,但它确实调用了非开源的 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler,它通常会做一个很多日志记录,包括详细原因(例如(在字符串中带有IDX10209-type 错误代码),但我不知道为什么它没有输出我可以捕获的任何内容。
如何记录来自JwtSecurityTokenHandler 的消息?
【问题讨论】:
标签: asp.net asp.net-core jwt