IdentityServer4授权始终获取"未找到签名密钥"在Azure AppService上

时间:2016-09-01 00:08:29

标签: asp.net-core azure-web-sites bearer-token .net-4.5.2 identityserver4

我有一个基于IS4 Identity样本的IdentityServer4应用程序,以及一个使用承载令牌的API,通过IS4.AccessTokenValidation进行授权。这可以通过VisualStudio在localhost上正常工作,当我部署到Windows 2012 VM并通过IIS托管时。当我将身份服务器作为App Service网站部署到Azure时,一切都很好。但是,当使用与VM相同的域和证书将API部署为App Service时,任何具有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的相同承载令牌可能会导致此错误。

1 个答案:

答案 0 :(得分:3)

事实证明,您需要在IssuerSigningKey中明确设置TokenValidationParameters。所以我从App Service商店获得证书,并通过JwtBearerOptions.TokenValidationParameters添加。所以Startup配置如下所示:

        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 App Service上需要它,而不是在服务器或开发机器上。其他人可以解释一下吗?它会建议Appate Service的ValidateIssuerSigningKey默认为true,其他地方则为false。