// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityModel.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace Client
{
public class Program
{
private static async Task Main()
{
// discover endpoints from metadata
var client = new HttpClient();
//var disco = await client.GetDiscoveryDocumentAsync("http://10.128.39.41:5001");
var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = "http://10.128.39.41:5001", Policy = new DiscoveryPolicy { RequireHttps = false } });
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 = "api1"
});
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n\n");
// call api
var apiClient = new HttpClient();
apiClient.SetBearerToken(tokenResponse.AccessToken);
var response = await apiClient.GetAsync("http://10.128.39.41:6001/identity");
if (!response.IsSuccessStatusCode)
{
Console.WriteLine(response.StatusCode);
}
else
{
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(JArray.Parse(content));
}
}
}
}
关键是这句:
var disco = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest { Address = "http://10.128.39.41:5001", Policy = new DiscoveryPolicy { RequireHttps = false } });
标签:Console,WriteLine,取消,await,disco,HTTPS,var,new,IdentityServer4
From: https://www.cnblogs.com/shiningrise/p/18420713