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

Retrieving authentication token for web app submitting to Dynamics CRM API?

(0) ShareShare
ReportReport
Posted on by

I am sending requests from a web app to the Dynamics CRM API, I want to allow the web app to send requests without the user authenticating as an Azure Active Directory user.  Are there any tools on Azure or within Dynamics that would allow me to do this?  Or is there any way to gather a token so that the application can send?  

Thanks in advance!

I have the same question (0)
  • fiorine Profile Picture
    on at

    You can create an application in azure that has a secret, then create an application user in crm.  Your app can then use the clientid and secret to connect to the CDS. 

  • Community Member Profile Picture
    on at

    Would that user then allow me to make cases within the CRM environment?  And how would I pass the secret to the user to be verified, just as a bearer in the authorization header?  Also I don't see an option within the user to add client secret. Thanks for your response!

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Follow below article to Register Application in Azure Active DIrectory.

    docs.microsoft.com/.../walkthrough-register-app-azure-active-directory

    Once this setup is completed, copy Application ID and Create Application use in CRM then grant appropriate permission/security role.

    Manually create a Common Data Service application user

    The procedure to create this user is different from creating a licensed user. Use the following steps:

    1. Navigate to Settings > Security > Users

    2. In the view drop-down, select Application Users.

    3. Click New. Then verify that you are using the Application user form.

      If you do not see the Application ID, Application ID URI and Azure AD Object ID fields in the form, you must select the Application User form from the list:

      pastedimage1595643490938v1.png

    4. Add the appropriate values to the fields:

      TABLE 3
      Field Value
      User Name A name for the user
      Application ID The Application ID value for the application registered with Azure AD.
      Full Name The name of your application.
      Primary Email The email address for the user.

      The Application ID URI and Azure AD Object ID fields are locked and you cannot set values for these fields.

      When you create this user the values for these fields will be retrieved from Azure AD based on the Application ID value when you save the user.

    5. Associate the application user with the custom security role you created.

    See this link here for more details - https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/authenticate-oauth

    class SampleProgram
    {
        private static string serviceUrl = "https://yourorg.crm.dynamics.com"; 
        private static string clientId = "51f81489-12ee-4a9e-aaae-a2591f45987d"; 
        private static string userName = "you@yourorg.onmicrosoft.com";
        private static string password = "yourpassword";
    
        static void Main(string[] args)
        {
    
            AuthenticationContext authContext = 
            new AuthenticationContext("https://login.microsoftonline.com/common", false);  
            UserCredential credential = new UserCredential(userName, password);
            AuthenticationResult result = authContext.AcquireToken(serviceUrl, clientId, credential);
            //The access token
            string accessToken = result.AccessToken;
    
            using (HttpClient client = new HttpClient()) {
            client.BaseAddress = new Uri(serviceUrl);
            client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  
            client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
            client.DefaultRequestHeaders.Add("OData-Version", "4.0");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage request = 
                new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/WhoAmI");
            //Set the access token
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            HttpResponseMessage response = client.SendAsync(request).Result;
            if (response.IsSuccessStatusCode)
            {
                //Get the response content and parse it.  
                JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                Guid userId = (Guid)body["UserId"];
                Console.WriteLine("Your system user ID is: {0}", userId);
            }
    
        }
    }

    If found helpful, Please mark my answer verified.

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 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans