Tutorials


    Before you start

    All code snippets in these tutorials are in CSharp (C#). The sample code uses the .Net “System.Net” & Microsoft MSAL library for .Net” libraries to connect to the Livestock identity system and access API HTTP resources.

    You can find a working code sample in our public GitHub repositories. The example application is a C# windows presentation foundation application and is available at below URL

    These code snippet in this tutorial and the same WPF application in the GitHub repositories use a simple HelloWorld API that exposes just 1 endpoint. The Api endpoint path is:

    • public/hello-world/hello-world (application authorises it using OAuth 2.0 authorisation grant flow)

    The endpoint exposes a Http GET method and returns the message 'Hello-World' upon successful invocation.

    Getting your subscription key

    You'll need a LIS API subscription key before making a call to the Livestock API Http endpoint. You can obtain these by creating an application and subscribing to an API. Please read about how to develop your application for more information.

    Getting your user credentials

    User credentials will be available once your application has been created. It's important you copy these, once they become available.

    Getting the right NuGet packages

    You'll need to add below NuGet packages in your application in order to consume MSAL library and the sample code in order to make call to the Livestock API endpoint.

    • Microsoft.Identity.Client
    • System.Windows
    • System.Windows.Interop
    • System.Net.Http

    Accessing a LI secure API endpoint

    In this example, you will access the Livestock example APIs ‘Hello-World’ endpoint.

    You would need to send a subscription key as well the user/access token when making a call to the hello-world endpoint. The endpoint is a user-restricted endpoint and a user will have to authorize the API call by providing a user name and a password when a pop-up dialog appears.

    Initialize MSAL App

    To begin the flow, you'll need to configure your MSAL Application

    PublicClientApp = PublicClientApplicationBuilder.Create(ClientId) .WithB2CAuthority(AuthoritySignUpSignIn) .WithRedirectUri(RedirectUri) .WithLogging(Log, LogLevel.Info, false) .Build();

    The highlighted (yellow) values will be provided to you by LI as part of the on-boarding process.

    Authenticating the user

    To authorise the user, your app must send the user to the authorisation URL provided by the LI support team. The scope details would also be provided on the specific API page.

    1. Acquire user id token interactively:

      AuthenticationResult authResult = null; authResult = await app.AcquireTokenInteractive(App.ApiScopes) .WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) .ExecuteAsync();

      The user will be redirected to the Livestock Identity login screen. When they've entered their credentials, the app will return with a valid identity token.

      Note: Silent retrieval of the id token [Ropc Flow] is not supported

    2. Get the access token

      Retrieve the access token before making a call to the API endpoint by making below call

      authResult = await app.AcquireTokenSilent(App.ApiScopes, accounts.FirstOrDefault()) .ExecuteAsync();
    3. Make a GET request to the ‘Livestock’ API hello-world endpoint:

      var httpClient = new HttpClient();HttpResponseMessage response; var request = new HttpRequestMessage(HttpMethod.Get, URL); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); request.Headers.Add("Ocp-Apim-Subscription-Key",new []{ ConfigurationManager.AppSettings["APISubscriptionKey"] }); response = await httpClient.SendAsync(request); var content = await response.Content.ReadAsStringAsync();

      The highlighted (yellow) values will be provided to you by LI as part of the on-boarding process.