Last active
December 7, 2017 11:17
-
-
Save HamidMosalla/721b38309a9472c2e0903916cfa2433d to your computer and use it in GitHub Desktop.
This file contains 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
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