IdentityServer3.AccessTokenValidation API和IdentityServer4

时间:2016-11-05 21:02:49

标签: asp.net-web-api identityserver3 identityserver4

我从IdSrv4获取访问令牌,当我尝试使用该令牌调用我的api时

var client = new HttpClient();
client.SetBearerToken(token.AccessToken);
var response = await client.GetAsync("http://localhost:60602/api/users");

我收到此错误消息:

  

Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationMiddleware   错误:0:身份验证失败System.InvalidOperationException:   序列中不包含任何元素   System.Linq.Enumerable.First [TSource](IEnumerable的1 source) at IdentityServer3.AccessTokenValidation.DiscoveryDocumentIssuerSecurityTokenProvider.<RetrieveMetadata>b__1(JsonWebKey key) in c:\local\identity\server3\AccessTokenValidation\source\AccessTokenValidation\Plumbing\DiscoveryDocumentIssuerSecurityTokenProvider.cs:line 152 at System.Linq.Enumerable.WhereSelectListIterator 2.MoveNext()   在   System.IdentityModel.Tokens.JwtSecurityTokenHandler.ResolveIssuerSigningKey(字符串   token,SecurityToken securityToken,SecurityKeyIdentifier   keyIdentifier,TokenValidationParameters validationParameters)at   System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(字符串   令牌,TokenValidationParameters validationParameters)at   System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(字符串   securityToken,TokenValidationParameters validationParameters,   SecurityToken&安培; validatedToken)at   Microsoft.Owin.Security.Jwt.JwtFormat.Unprotect(String protectedText)   在   Microsoft.Owin.Security.OAuth.OAuthBearerAuthenticationHandler.d__0.MoveNext()

我读了这个问题并添加了此代码生成的证书 https://github.com/ElemarJR/LearningIdentityServer4/tree/master/LearningIdentityServer.OAuth

但没有成功。

WebApi代码 ...

 app.UseCookieAuthentication(new CookieAuthenticationOptions());
 app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
 JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();
 app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
 {
       Authority = "http://localhost:5000",//Constants.BaseAddress,
       RequiredScopes = new[] { "api1" },
 });

...

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我有同样的问题;阅读以下链接 [在此输入链接描述] [1]

  

Identity server 4 token not validate in .NetFramework Api that use Identity Server 3

  

https://github.com/IdentityServer/IdentityServer3.AccessTokenValidation/issues/124

总结一下,你必须将identityserver3.accesstokenvalidation升级为&#34; v2.13.0&#34;