【发布时间】:2019-03-26 14:09:31
【问题描述】:
我使用 Identity Server 4 实现了一个令牌服务器。
我向令牌服务器添加了一个自定义 API 端点,并在身份验证方面遇到了困难。自定义端点继承自 ControllerBase,具有 3 种方法(GET、POST、DELETE)。
我打算使用具有凭据(服务器到服务器)的专用客户端在另一个 API 中调用自定义端点,该客户端在 .NET Core 中实现为 HttpClient。没有用户参与其中。
为了获取访问令牌,我使用了 IdentityModel DiscoveryClient 和 TokenEndpoint。
所以到目前为止我做了以下事情:
- 设置“常规”身份服务器并验证它是否有效 -> 有效
- 实现自定义端点并在未经授权的情况下对其进行测试 -> 可以正常工作
- 添加另一个具有自定义范围“api.auth.endpoint1”的 api 资源(“api.auth”)
- 使用允许访问范围“api.auth.endpoint1”的客户端凭据设置客户端。
- 实现 HttpClient 和测试设置 -> 我通过身份模型令牌端点获得访问令牌。
现在,当我使用收到的访问令牌使用 HttpClient 调用端点时,我得到响应代码 200(OK),但内容是身份服务器的登录页面。
documentation of Identity Server 4状态使用
services.AddAuthentication()
.AddIdentityServerAuthentication("token", isAuth =>
{
isAuth.Authority = "base_address_of_identityserver";
isAuth.ApiName = "name_of_api";
});
以及使用
[Authorize(AuthenticationSchemes = "token")]
不幸的是,编译器指出 .AddIdentityServerAuthentication 无法找到。我错过了一个特殊的 nuget 吗?
到目前为止,我在令牌服务器上使用的 nuget 是:
- IdentityServer4 (v2.2.0)
- IdentityServer4.AspNetIdentity (v2.1.0)
- IdentityServer4.EntityFramework (v2.1.1)
想通了那部分。 AddIdentityServerAuthentication 缺少的 nuget 是:
- IdentityServer4.AccessTokenValidation
在自定义范围的授权上苦苦挣扎。
有人知道如何配置安全性吗?
【问题讨论】:
标签: asp.net-core oauth-2.0 .net-core identityserver4