5

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

Www-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"

我们使用 .NET 4.5.2,最新版本的 IdentityServer4 和 IdentityServer4.AccessTokenValidation 包。我还从 2016 年 8 月 30 日从 GitHub 中提取了最新的这些包,没有任何变化。无论如何,我不认为 IS4 Validator 是一个错误,但我不知道是什么原因造成的。有什么建议么?它是 Azure 主机错误吗?

我希望能够对此进行调试,但即使我从头开始重建,我也无法让远程调试对这个应用程序起作用,并且应用程序日志什么也没告诉我。我在 ASP.NET 安全存储库中翻找过,但没有更多的日志记录或调试访问权限,我对如何解决这个问题一无所知。

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);

并且 Identity Server 是直接从样本中提取出来的,并且使用购买的证书进行签名。

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

4

1 回答 1

4

事实证明,您需要明确设置IssuerSigningKeyin TokenValidationParameters。所以我从 App Service 商店获取证书,并通过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();
        ...
    }

不知道为什么只需要在 Azure 应用服务上而不是在服务器或开发机器上。其他人可以解释一下吗?它会建议 ValidateIssuerSigningKey 默认为应用服务的 true 和 false 其他任何地方。

于 2016-09-01T15:06:16.590 回答