web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Connect C# console with Web Api dynamics 365 CRM online

(0) ShareShare
ReportReport
Posted on by

Hi Expects,

 I am new to console app . using OAuth, I am trying to connect to crm and retrieve all accounts records as show in below code. 

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;

namespace CRMConnection
{
class Program
{
static void Main(string[] args)
{
var accounts = CrmRequest(HttpMethod.Get,"crmurl/api/data/v9.1/accounts").Result.Content.ReadAsStringAsync();
}

/// Method-to-generate-Access-Token
public static async Task<string> AccessTokenGenerator()
{
string clientId = "ID";
string clientSecret = "Secret code";
string authority = "login.microsoft.com/tenantID/oauth2/v2.0/token";
string resourceUrl = "CRMUrl";

var credentials = new ClientCredential(clientId, clientSecret);
var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);
return result.AccessToken;
}

public static async Task<HttpResponseMessage> CrmRequest(HttpMethod httpMethod, string requestUri, string body = null)
{
var accessToken = await AccessTokenGenerator();
var client = new HttpClient();
var msg = new HttpRequestMessage(httpMethod, requestUri);
msg.Headers.Add("OData-MaxVersion", "4.0");
msg.Headers.Add("OData-Version", "4.0");
msg.Headers.Add("Prefer", "odata.include-annotations=\"*\"");

// Passing AccessToken in Authentication header
msg.Headers.Add("Authorization", $"Bearer {accessToken}");

if (body != null)
msg.Content = new StringContent(body, UnicodeEncoding.UTF8, "application/json");
return await client.SendAsync(msg);
}
}
}

I am getting below error : The program '[24580] CRMConnection.exe' has exited with code 0 (0x0).

Reason : I need to check the roles in another call and assgin system admin access . can any one please provide the code how to acheive this ?

I have the same question (0)
  • Suggested answer
    Menna El Sayed Galal Profile Picture
    on at

    Hello,

    In order this Achieve this you will need to follow these documentations from starting to register an application and end with the sample code 

    https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/authenticate-oauth

    if you still don't able to connect , I would recommend to open a support ticket as it will need an online session with screen sharing to check the azure configurations and user account in CRM and the code as well as a first step in troubleshooting.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans