Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. You want to install the IdentityModel package from the NuGet feed into your client project.
  2. This will enable you to shortcut some of the URL mapping and discovery in the OAuth workflow. The following will do this work for you

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleDiscovery of IDs IdentityServer Metadata Code Example
    linenumberstrue
    collapsetrue
    // discover endpoints from metadata
    var disco = await DiscoveryClient.GetAsync("https://warden.data.uwaterloo.ca");
    if (disco.IsError)
    {
        Console.WriteLine(disco.Error);
        return;
    }
  3. Next you'll pass your credentials to the token server and receive back a JSON response that will include your authorization token.

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleGetting Token from IdentityServer Code Example
    linenumberstrue
    collapsetrue
    var tokenClient = new TokenClient(disco.TokenEndpoint, "your_client_id", "your_client_password");
    var tokenResponse = await tokenClient.RequestClientCredentialsAsync("sourceapi");
    
    if (tokenResponse.IsError)
    {
        Console.WriteLine(tokenResponse.Error);
        return;
    }
    
    Console.WriteLine(tokenResponse.Json);
  4. Now with your valid token you'll be able to make calls against the API to get data.
  5. To send the access token to the API you typically use the HTTP Authorization header. This is done using the SetBearerToken extension method.

    Code Block
    languagec#
    themeConfluence
    firstline1
    titleCalling the API with Token Code Example
    linenumberstrue
    collapsetrue
    var client = new HttpClient();
    client.SetBearerToken(tokenResponse.AccessToken);
    
    var response = await client.GetAsync("https://api.data.uwaterloo.ca/api/v1/terms");
    if (!response.IsSuccessStatusCode)
    {
        Console.WriteLine(response.StatusCode);
    }
    else
    {
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine(content);
    }

...

Code Block
languagec#
titleC# HttpClient Code Example
linenumberstrue
collapsetrue
static void Main(string[] args)
        {
            // Create client to get Identity Server Response
            var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Add required information to retrieve token (you will need to use your credentials)
            var content = new FormUrlEncodedContent(new[] {
                new KeyValuePair<string, string>("scope", "sourceapi"),
                new KeyValuePair<string, string>("grant_type", "client_credentials"),
                new KeyValuePair<string, string>("client_id", "your_client_id"),
                new KeyValuePair<string, string>("client_secret", "your_client_secret")
            });

            // Get response from a POST 
            var response = client.PostAsync("https://warden.data.uwaterloo.ca/connect/token", content).Result;
            response.EnsureSuccessStatusCode();

            // For our purposes we'll get the return as string (that is valid JSON) and then deserialize into an object
            var jsonResponseString = response.Content.ReadAsStringAsync().Result;
            var jsonObject = JsonConvert.DeserializeObject<JwtResponseBlock>(jsonResponseString);

            client.Dispose();

            // Query the API by setting the Authorization header value with the token we got
            client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jsonObject.access_token);

            // Retrieve Term informatino from API as an example
            var termsJsonString = client.GetStringAsync("https://api.data.uwaterloo.ca/api/v1/terms").Result;
            
        }

        private class JwtResponseBlock
        {
            public string access_token { get; set; }            
        }

...