Skip to content

Instantly share code, notes, and snippets.

@skynyrd
Last active January 10, 2019 12:59
Show Gist options
  • Save skynyrd/3a318b4990419f98fffb26600c048ff0 to your computer and use it in GitHub Desktop.
Save skynyrd/3a318b4990419f98fffb26600c048ff0 to your computer and use it in GitHub Desktop.
class Program
{
public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
private static async Task MainAsync()
{
// discover endpoints from metadata
var disco = await DiscoveryClient.GetAsync("http://localhost:5000"); //IdentityServer Address
if (disco.IsError)
{
Console.WriteLine(disco.Error);
return;
}
// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "TheClientId", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("TheApi");
if (tokenResponse.IsError)
{
Console.WriteLine(tokenResponse.Error);
return;
}
Console.WriteLine("Token Response: \n");
Console.WriteLine(tokenResponse.Json);
Console.WriteLine("\n**************************");
// call api
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var response = await client.GetAsync("http://localhost:5001/api/values");
Console.WriteLine("Status code from the API:\n");
Console.WriteLine(response.StatusCode);
Console.WriteLine("\n**************************");
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Response body: ");
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment