Go to my github

ASP.NET Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记

任务10:第三方ClientCredential模式调用

创建一个控制台程序

dotnet new console --name ThirdPartyDemo

添加 Nuget 包:IdentityModel

添加之后还原

dotnet restore

Client

using System;
using System.Net.Http;
using System.Threading.Tasks;
using IdentityModel.Client;
using Newtonsoft.Json.Linq;

namespace ThirdPartyDemo
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // discover endpoints from metadata
            var client = new HttpClient();
            var disco = client.GetDiscoveryDocumentAsync("http://localhost:5000").Result;
            if (disco.IsError)
            {
                Console.WriteLine(disco.Error);
                return;
            }

            // request token
            var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address = disco.TokenEndpoint,

                ClientId = "client",
                ClientSecret = "secret",
                Scope = "api"
            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.Json);

            // call api
            var client2 = new HttpClient();
            client2.SetBearerToken(tokenResponse.AccessToken);

            var response = await client2.GetAsync("http://localhost:5001/weatherforecast");
            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode);
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();
                Console.WriteLine(JArray.Parse(content));
            }
        }
    }
}


先启动 IdentityServerCenter,ClientCredentialApi

再启动 ThirdPartyDemo,输出如下:

{
  "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImZFd0d5VGQtY2FkaE9Oamp6ajc5THciLCJ0eXAiOiJhdCtqd3QifQ.eyJuYmYiOjE1ODYyNzcwNTYsImV4cCI6MTU4NjI4MDY1NiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjoiYXBpIiwiY2xpZW50X2lkIjoiY2xpZW50Iiwic2NvcGUiOlsiYXBpIl19.KRgYum2zuAdqNkzm9rMfIh7ARMPJHLZw_k55AU-wxQEYfhc6ZAiJkayRY98gKJb--nvblpBTE4u5erJNUSBBGXriMsohemvVh-8pA72PkVzNJ9KuDAUX3VagsphQ36-ZEf7lq1V87y0Fh3higUsFTeyEa8D1pZQncS6GzlRel-Q7ghX20tVl0pWdvN1BnG06AdU9-1l15OoKg9aDRL9oKf4MO9zvJhJhmXazigvrWTKEcTk7gUVs-NW8rxBwNagTC386vVSatd5qmAhph4eDA8Ryjf9utE8qEDD6cuX2qFJn0yKxpFm2zn9AMUs5YmKMesUddKeocdk9DAIXIdqehw",
  "expires_in": 3600,
  "token_type": "Bearer",
  "scope": "api"
}
[
  {
    "date": "2020-04-09T00:30:56.3318938+08:00",
    "temperatureC": -12,
    "temperatureF": 11,
    "summary": "Hot"
  },
  {
    "date": "2020-04-10T00:30:56.3328057+08:00",
    "temperatureC": 2,
    "temperatureF": 35,
    "summary": "Cool"
  },
  {
    "date": "2020-04-11T00:30:56.3328097+08:00",
    "temperatureC": 27,
    "temperatureF": 80,
    "summary": "Sweltering"
  },
  {
    "date": "2020-04-12T00:30:56.33281+08:00",
    "temperatureC": 35,
    "temperatureF": 94,
    "summary": "Balmy"
  },
  {
    "date": "2020-04-13T00:30:56.3328102+08:00",
    "temperatureC": 23,
    "temperatureF": 73,
    "summary": "Mild"
  }
]

参考文档:

http://docs.identityserver.io/en/latest/quickstarts/1_client_credentials.html#creating-the-client

课程链接

http://video.jessetalk.cn/course/explore

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

posted @ 2020-04-08 00:35  郑子铭  阅读(370)  评论(0编辑  收藏  举报