Skip to content

Instantly share code, notes, and snippets.

Embed
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 : http://www.wrapcode.com/server-authentication-dynamics-crm/
/// 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
try
{
authResult = await authContext.AcquireTokenAsync("https://{{TENANT}}.crm4.dynamics.com", clientCreds);
// Add the access token to the authorization header of the request.
if(authResult == null)
return;
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
HttpResponseMessage response = await httpClient.GetAsync("https://{{TENANT}}.crm4.dynamics.com/api/data/v8.1/contacts?$top=1");
if (response.IsSuccessStatusCode)
{
// Read the response and output it to the console.
string s = await response.Content.ReadAsStringAsync();
}
else
{
Console.WriteLine("Failed to retrieve contact \nError: {0}\n", response.ReasonPhrase);
}
}
catch (Exception ex)
{
_logger.Fatal(ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.