如何从 IdentityServer4 调用受保护的 API?

Mop*_*opa 1 identityserver4

我想通过调用由 IS 验证的单独 api 将电子邮件服务与身份服务器项目分开。

我的问题是从身份服务器调用 api。目前,我在 IdentityServer 中有额外的“假”客户端正在调用 api。

这是客户端凭据流程的标准选项。

  1. 获取代币
  2. 返回令牌
  3. 调用API
  4. 检查令牌
  5. 返回数据

标准客户端凭证流程 但我想省略客户端。所以IdentityServer是客户端。我在 IS 内部创建了一个“假”客户端,我认为这是错误的做法?

IdentityServer 内的 MessageService.cs

 // Authenticating the fake client
 var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
 var tokenClient = new TokenClient(disco.TokenEndpoint, "MailApiClient", "secret");
 var tokenResponse = await tokenClient.RequestClientCredentialsAsync("EmailScope");
 // Setting the token
 client.SetBearerToken(tokenResponse.AccessToken);
 var content = new StringContent(JsonConvert.SerializeObject(someMailModel));
 // Posting to api
 var response = await client.PostAsync(_emailSettings.RequestUri, content).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)

IS 内部的客户端

假客户端通过 IS 进行身份验证,然后将带有令牌的消息发送到 mailapi。我希望 IS 本身就是客户端,而不是在 IS 内部创建假客户端。因此IdentityServer将在没有中间件客户端的情况下调用api。

将来其他一些客户端也将使用 MailApi。

Mop*_*opa 5

有一个类“IdentityServerTools”,它提供了在 IdentityServer 内手动颁发令牌的能力。

如何保护 IdentityServer4 调用的 Web API https://identityserver4.readthedocs.io/en/release/topics/tools.html