Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using IdentityModel.Client;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace FastandMax
- {
- public class Program
- {
- private static async Task Main()
- {
- // discover endpoints from metadata
- var client = new HttpClient();
- var disco = await client.GetDiscoveryDocumentAsync("http://localhost:5000");
- if (disco.IsError)
- {
- Console.WriteLine(disco.Error);
- return;
- }
- // request token
- var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
- {
- Address = disco.TokenEndpoint,
- ClientId = "ro.client",
- ClientSecret = "secret",
- UserName = "alice",
- Password = "Pass123$",
- 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://localhost:5001/identity");
- if (!response.IsSuccessStatusCode)
- {
- Console.WriteLine(response.StatusCode);
- }
- else
- {
- var content = response.Content.ReadAsStringAsync().Result;
- Console.WriteLine(JArray.Parse(content));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement