IdentityServer4 Authorize 在 Azure AppService 上始终收到“找不到签名密钥”

use*_*195 5 bearer-token .net-4.5.2 azure-web-app-service asp.net-core identityserver4

我有一个基于 IS4 Identity 示例的 IdentityServer4 应用程序,以及一个使用不记名令牌通过 IS4.AccessTokenValidation 进行授权的 API。这通过 VisualStudio 在本地主机上运行良好,当我部署到 Windows 2012 VM 并通过 IIS 托管时,运行良好。当我将身份服务器作为应用服务网站部署到 Azure 时,一切都很好。但是,当使用与 VM 相同的域和证书将 API 部署为应用服务时,任何具有 Authorize 属性的方法(无论是否有策略)始终返回 401 和标头消息:

Www-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"
Run Code Online (Sandbox Code Playgroud)

我们使用 .NET 4.5.2 以及最新版本的 IdentityServer4 和 IdentityServer4.AccessTokenValidation 包。我还从 2016 年 8 月 30 日起从 GitHub 中提取了这些软件包的最新版本,没有任何更改。无论如何,我不认为这是 IS4 Validator 的错误,但我不知道是什么原因导致的。有什么建议么?这是 Azure 主机错误吗?

我希望能够调试这个,但即使我从头开始重建,我也无法让远程调试工作到这个应用程序,并且应用程序日志没有告诉我任何信息。我翻遍了 ASP.NET Security 存储库,但没有更多的日志记录或调试访问权限,我对如何解决此问题一无所知。

API 配置非常基本:

    var jwtBearerOptions = new JwtBearerOptions()
        {
            Authority = Configuration["Authentication:IdentityServer:Server"],
            Audience = Configuration["Authentication:IdentityServer:Server"]+"/resources",
            RequireHttpsMetadata = false,

            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
        };
        app.UseJwtBearerAuthentication(jwtBearerOptions);
Run Code Online (Sandbox Code Playgroud)

Identity Server 直接来自示例,并使用购买的证书进行签名。

还有其他人将此配置完全用作 2 个 Azure 应用服务吗?或者,如果发送到 VM 托管 API 的相同不记名令牌是可接受的,则可能导致此错误的原因是可以接受的。

use*_*195 5

事实证明,您需要显式设置IssuerSigningKeyin TokenValidationParameters。因此,我从应用程序服务商店获取证书,并通过JwtBearerOptions.TokenValidationParameters. 所以启动配置如下所示:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        ...
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
        var tokenValidationParameters = new TokenValidationParameters
        {
            // The signing key must match!
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = new X509SecurityKey(GetSigningCertificate()),

            // Validate the JWT Issuer (iss) claim
            ValidateIssuer = false,
            //ValidIssuer = "local",

            // Validate the JWT Audience (aud) claim
            ValidateAudience = false,
            //ValidAudience = "ExampleAudience",

            // Validate the token expiry
            ValidateLifetime = true,

            // If you want to allow a certain amount of clock drift, set that here:
            ClockSkew = TimeSpan.Zero
        };
        var jwtBearerOptions = new JwtBearerOptions()
        {
            Authority = Configuration["Authentication:IdentityServer:Server"],
            Audience = Configuration["Authentication:IdentityServer:Server"]+"/resources",
            RequireHttpsMetadata = false,

            AutomaticAuthenticate = true,
            AutomaticChallenge = true,

            TokenValidationParameters = tokenValidationParameters
        };
        app.UseJwtBearerAuthentication(jwtBearerOptions);

        app.UseMvc();
        ...
    }
Run Code Online (Sandbox Code Playgroud)

不知道为什么只在 Azure 应用服务上需要,而不是在服务器或开发计算机上。还有人能解释一下吗?它建议 ValidateIssuerSigningKey 对于应用服务默认为 true,而在其他地方默认为 false。