ASP.NET Core combate proyecto distribuido (tercero ClientCredential RECALL) - Notas de Estudio

Tarea 10: modo de llamada de terceros ClientCredential

Crear una aplicación de consola

dotnet new console --name ThirdPartyDemo

Añadir paquete Nuget: IdentityModel

Después de añadir la reducción

dotnet restore

Cliente

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


En primer lugar empezar IdentityServerCenter, ClientCredentialApi

ThirdPartyDemo reinicio, salida:

{
  "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"
  }
]

Documentos de referencia:

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

enlace de curso

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

Licencia Creative Commons

Este trabajo es Creative Commons Reconocimiento - No comercial - Compartir bajo la misma licencia 4.0 Acuerdo Internacional para la licencia.

Bienvenido a imprimir, utilizar, publicar, pero asegúrese de mantener el artículo firmado por Zheng Ziming (que contiene enlaces: http://www.cnblogs.com/MingsonZheng/ ), no serán utilizados con fines comerciales, asegúrese de publicar el mismo trabajo basado en la modificación de licencia de papel .

Si tienes alguna pregunta, por favor, póngase en contacto conmigo ([email protected]).

Supongo que te gusta

Origin www.cnblogs.com/MingsonZheng/p/12657373.html
Recomendado
Clasificación