【发布时间】:2019-04-27 04:11:45
【问题描述】:
我们已经使用 .Net Core 2.1 设置了身份服务器 4。我们在 .Net 4.6 上有 ASP.Net Web API。这不能迁移到核心。我已经在这个 API 项目中安装了 IdentityServer3.AccessTokenValidation 以及 OWN 和其他需要的包。我可以使用客户端并从 IdentityServer 获取访问令牌。当客户端将该令牌与 API 一起使用时,我得到 401。我在网上搜索了解决方案,但找不到任何我可以用来完成这项工作的东西。 我试过这个 - https://github.com/IdentityServer/IdentityServer3.AccessTokenValidation/issues/164 但没有用。我找不到任何样品。 所以我的问题是不能使用 IdentityServer3.AccessTokenValidation 与 IDS4 对话吗?
我需要设置 IDS3 吗?
更新
我打开详细日志记录并得到以下信息
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44333/connect/introspect application/x-www-form-urlencoded 70
IdentityServer4.Hosting.IdentityServerMiddleware:Information: Invoking IdentityServer endpoint: IdentityServer4.Endpoints.IntrospectionEndpoint for /connect/introspect
IdentityServer4.Validation.ApiSecretValidator:Error: API validation failed.
IdentityServer4.Endpoints.IntrospectionEndpoint:Error: API unauthorized to call introspection endpoint. aborting.
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 26.5186ms 401
Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLIJ2C0UKBLN", Request id "0HLIJ2C0UKBLN:00000008": the application completed without reading the entire request body.
下面是我在配置方法中的API代码
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "https://localhost:44333",
RequiredScopes = new[] { "api" },
ClientId = "api",
ClientSecret = "secret",
//DelayLoadMetadata = true
});
app.UseWebApi(config);
IDS4 端用于 ApiResource 的代码。
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api", "MyApi") { ApiSecrets = { new Secret("secret".Sha256()) } }
};
}
更新 2
是的,我已启用 Trace。在更多地使用代码并重新安装软件包之后,我现在得到了一个不同的错误。请求已发送到 /connect/accesstokenvalidation。此端点在 IDS4 中不可用
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: 请求开始 HTTP/1.1 POST http://localhost:44333/connect/accesstokenvalidation application/x-www-form-urlencoded 70 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求在 3.405 毫秒内完成 404 Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLIJ9RBG591K", Request id "0HLIJ9RBG591K:0000000A": 应用程序在没有读取整个请求体的情况下完成。
【问题讨论】:
-
没有理由不工作。 IDS3 或 4 都使用相同的协议。你一定有什么配置错误。要进行调试,请创建一个控制台客户端应用程序并使用它来访问 IDS4。
-
你有样品吗?我可以使用客户端从 IDS4 获取访问代码。但是,当客户端使用令牌与 API 对话时,它不起作用。我确实尝试创建与 IdentityServer4.AccessTokenValidation 一起使用的 ASP.Net Web API Core。但是在我的情况下,我不能为 API 项目使用 ASP.NET Core
-
通常我会在IDS4中开启verbose debug log,你可能会在日志中找到线索。
-
请查看更新日志和代码 sn-p
-
如果我删除 DelayLoadMetadata = true,我会收到以下错误 System.TypeLoadException: 'Could not load type 'Microsoft.Owin.Security.Jwt.IIssuerSecurityTokenProvider' from assembly 'Microsoft.Owin.Security.Jwt' , 版本=4.0.0.0, 文化=中性, PublicKeyToken=31bf3856ad364e35'.'
标签: asp.net-core asp.net-web-api2 identityserver4 identityserver3