Created
July 5, 2017 11:36
-
-
Save abel-masila/9ffe13dd6c22761a7d87683e8e9bbbab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
{ | |
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"); | |
// request token | |
var tokenClient = new TokenClient(disco.TokenEndpoint, "client", "secret"); | |
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1"); | |
if (tokenResponse.IsError) | |
{ | |
Console.WriteLine(tokenResponse.Error); | |
return; | |
} | |
Console.WriteLine(tokenResponse.Json); | |
Console.WriteLine("\n\n"); | |
// call api | |
var client = new HttpClient(); | |
client.SetBearerToken(tokenResponse.AccessToken); | |
var response = await client.GetAsync("http://localhost:5001/identity"); | |
if (!response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine(response.StatusCode); | |
} | |
else | |
{ | |
var content = await response.Content.ReadAsStringAsync(); | |
Console.WriteLine(JArray.Parse(content)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment