Skip to content

Instantly share code, notes, and snippets.

@HamidMosalla
Last active December 7, 2017 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HamidMosalla/721b38309a9472c2e0903916cfa2433d to your computer and use it in GitHub Desktop.
Save HamidMosalla/721b38309a9472c2e0903916cfa2433d to your computer and use it in GitHub Desktop.
public static async Task<string> RequestWithClientCredentialsWithPolicy()
{
async Task<string> GetAccessToken()
{
var openIdConnectEndPoint = await DiscoveryClient.GetAsync("http://localhost:5000");
var tokenClient = new TokenClient(openIdConnectEndPoint.TokenEndpoint, "client1", "123654");
var accessToken = await tokenClient.RequestClientCredentialsAsync("Api1");
if (accessToken.IsError)
{
Console.WriteLine(accessToken.Error);
return accessToken.Error;
}
Console.WriteLine(accessToken.Json);
return accessToken.AccessToken;
}
using (var client = new HttpClient())
{
var accessToken = await GetAccessToken();
client.SetBearerToken(accessToken);
var response = await client.GetAsync("http://localhost:5001/api/ApiResourceWithPolicy");
if (!response.IsSuccessStatusCode)
{
return response.StatusCode.ToString();
}
var content = await response.Content.ReadAsStringAsync();
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment