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

相关文章

ASP.NET Core分布式项目实战(客户端集成IdentityServer)–学习笔记

ASP.NET Core分布式项目实战(业务介绍,架构设计,oAuth2,IdentityServer4)–学习笔记

ASP.NET Core分布式项目实战(课程介绍,MVP,瀑布与敏捷)–学习笔记

ASP.NET Core快速入门 — 学习笔记汇总
欢迎各位读者加入微信群一起学习交流,在公众号后台回复“加群”即可~~

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

原文出处:微信公众号【郑子铭 DotNet NB】

原文链接:https://mp.weixin.qq.com/s/jYh63PH8NQ_-6oEyb1B3Cg

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论