【发布时间】:2016-11-05 21:02:49
【问题描述】:
我从 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.WhereSelectListIterator2.MoveNext() 在 System.IdentityModel.Tokens.JwtSecurityTokenHandler.ResolveIssuerSigningKey(字符串 令牌、SecurityToken securityToken、SecurityKeyIdentifier keyIdentifier,TokenValidationParameters 验证参数)在 System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(字符串 令牌,令牌验证参数验证参数)在 System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(字符串 securityToken, TokenValidationParameters 验证参数, 安全令牌和验证令牌)在 Microsoft.Owin.Security.Jwt.JwtFormat.Unprotect(字符串受保护文本) 在 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" },
});
...
有什么建议吗?
【问题讨论】:
标签: asp.net-web-api identityserver3 identityserver4