Smartitectures
'Simplicity is the ultimate sophistication' - Leonardo da Vinci

Architecture

Posts

(HRQ) ADAL and Console Applications

Here is the example console code from the video:

using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;

namespace ADALConsole
{
        class Program
    {
        static void Main(string[] args)
        {
            CallWebApiWithADALAsync("https://[...].crm.dynamics.com/api/data/v8.2/accounts").Wait();
        }
        static async Task CallWebApiWithADALAsync(string restEndpoint)
        {
            string resource = "https://[...].crm.dynamics.com";

            //App Registered in Tenant AADols
            string clientId = "57c1af48-[...]";

            string redirectUrl = "https://home.dynamics.com";
            string authority = "https://login.windows.net/common/oauth2/authorize";

            PlatformParameters parameters = new PlatformParameters(PromptBehavior.RefreshSession);
            try
            {
                AuthenticationContext authContext = new AuthenticationContext(authority);
                var token = await authContext.AcquireTokenAsync(resource, clientId, new Uri(redirectUrl), parameters);
                var authHeader = token.CreateAuthorizationHeader();
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(token.AccessTokenType, token.AccessToken);
                Uri requestURI = new Uri(restEndpoint);
                Console.WriteLine($"Calling REST API:'{requestURI}'.");
                HttpResponseMessage httpResponse = await client.GetAsync(requestURI);
                Console.WriteLine($"HTTP Code: '{httpResponse.StatusCode.ToString()}'");
                string response= await httpResponse.Content.ReadAsStringAsync();
                var json = JsonConvert.DeserializeObject(response);
                Console.WriteLine($"Response: {json}");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Message: " + ex.Message);
                Console.WriteLine("Stack: " + ex.StackTrace);
            }
            finally
            {
                Console.Read();
            }
        }
    }
}

Edit 10/12/2017: At 4m 09s, I am mention the Authority. The authority,  or authorization server, gets consent form the resource owner and issues the client token.

steve kurtz