3

我有一个基于IS4身份示例的IdentityServer4应用程序,以及使用承载令牌的API通过IS4.AccessTokenValidation进行授权。这通过VisualStudio在localhost上正常工作,当我部署到Windows 2012 VM并通过IIS托管时。当我将Identity Server作为App Service网站部署到Azure时,一切都很好。但是,当将API部署为使用与VM相同的域和证书的App Service时,具有Authorize属性的任何方法(带有策略或无,无关紧要)始终返回带有标头消息的401:IdentityServer4授权始终获取Azure AppService上的“未找到签名密钥”

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

我们正在使用.NET 4.5.2以及最新版本的IdentityServer4和IdentityServer4.AccessTokenValidation包。我也从G/08从30/08/16取下了最新的这些软件包,没有任何变化。我不认为这是一个错误IS4验证无论如何,但我不知道什么可能造成这一点。有什么建议么?它是一个Azure主机错误?

我希望能够进行调试,但即使从头开始重建,应用程序日志也不会告诉我任何东西,但我无法使Remote Debug工作到此应用程序。我已经在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); 

和身份Server是直出样品,并使用签名购买的证书。

是否有其他人将此配置完全用作2 Azure App Services?或者,如果发送到VM托管API的相同承载令牌可接受,可能会导致此错误。

+0

在https://gitter.im/IdentityServer/IdentityServer3/archives/2015/04/13上讨论了idsrv3的类似情况。我的理解是,没有获取身份服务器元数据('.well-known/opened-configuration')会出现问题。对于更多日志记录,在api中配置aspnet核心日志记录(您可以使用它来使用serilog写入日志文本)并检查错误日志。 –

+0

在该线程中关于令牌大小的有趣可能性。我的规模相当大,但我认为这是所有主机的问题。将有一个发挥它。你对伐木是正确的,我会加入Serilog。看看出了什么。 – user1587195

+0

获取更多日志记录是我真正需要的,并查看安全回购代码。出于某种原因,似乎必须在App Service的SigningKey验证选项上有所不同。 – user1587195

回答

3

原来你需要明确地设置IssuerSigningKeyTokenValidationParameters。所以我从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。

相关问题