代码之家  ›  专栏  ›  技术社区  ›  Mopa

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

  •  1
  • Mopa  · 技术社区  · 7 年前

    我想通过调用由is进行身份验证的单独api,将电子邮件服务从identity server项目中分离出来。

    我的问题是从identity server调用api。目前,我在IdentityServer中有另外一个调用api的“假”客户端。

    下面是客户端凭据流的标准选项。

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

    Standard Client Credential Flow 但我想省略客户。因此,IdentityServer就是客户端。 我在内部创建了一个“假”客户,我认为这样做是错误的?

    MessageService。cs内部IdentityServer

     // 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);
    

    client inside IS

    假客户端使用IS进行身份验证,然后将带有令牌的消息发送到mailapi。 相反,在内部创建假客户机是我想要的,是客户机本身。因此,IdentityServer将在没有中间件客户端的情况下调用api。

    此外,将来还会有其他一些客户端使用MailApi。

    1 回复  |  直到 7 年前
        1
  •  5
  •   Mopa    7 年前

    有一个类“IdentityServerTools”,它可以在IdentityServer中手动颁发令牌。

    How to protect Web API called by IdentityServer4 https://identityserver4.readthedocs.io/en/release/topics/tools.html