Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Headless communcation to Dynamics CRM using application user
/// <summary>
/// Headless communication to dynamics CRM using application user.
/// More information at :
/// This is just a sample code, there are way better ways to achieve the same. Did not want to spend much time on code snippet.
/// </summary>
public static async Task HeadlessAuthenticationPOC()
var clientCreds = new ClientCredential("{AZURE_AD_APP_ID}", "{AZURE_AD_APP_SECRET}");
AuthenticationResult authResult = null;
// Call sample API
authResult = await authContext.AcquireTokenAsync("https://{{TENANT}}", clientCreds);
// Add the access token to the authorization header of the request.
if(authResult == null)
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
HttpResponseMessage response = await httpClient.GetAsync("https://{{TENANT}}$top=1");
if (response.IsSuccessStatusCode)
// Read the response and output it to the console.
string s = await response.Content.ReadAsStringAsync();
Console.WriteLine("Failed to retrieve contact \nError: {0}\n", response.ReasonPhrase);
catch (Exception ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment