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,...
Unanswered

Error accessing Dynamics CRM via Dataverse API, worked but stopped working

(1) ShareShare
ReportReport
Posted on by 22
I was able to run the same App a few months ago, by using Microsoft.PowerPlatform.Dataverse.Client.ServiceClient to retrieve the DynamicsCRM entity, for example, "AccountData", but now I am getting this "The HTTP request was forbidden with client authentication scheme 'Anonymous'" even though I logged in via Azure AD (now Entra ID). 
 
I initiated the client:
_serviceClient = new ServiceClient(new Uri(config.Resource), config.ClientId, config.ClientSecret, true);
 
the Uri to Dynamics CRM service, client id and client secret are all valid, and it worked before I updated all the related NuGet packages to the latest:
Microsoft.AspNetCore.Authentication.JwtBearer (8.06)
Microsoft.AspNetCore.Authentication.OpenIdConnect (8.06)
Microsoft.Identity.Web (2.19.1)
Microsoft.Identity.Web.UI (2.19.1)
Microsoft.PowerPlatform.Dataverse.Client (1.1.22)
Microsoft.PowerPlatform.Dataverse.Client.Dynamics (1.1.22)
 
public async Task<IReadOnlyList<AccountData>> ReadAccounts(int accountId)
{
    var result = new List<AccountData>();

    var response = await _serviceClient.RetrieveMultipleAsync(new QueryExpression
    {
        EntityName = "AccountData",
        Distinct = true,
        Criteria =
        {
            Conditions =
            {
                new ConditionExpression("account_id", ConditionOperator.Equal, accountId)
            },
        },
        ColumnSet = new ColumnSet(allColumns: true)
    });

    foreach (var entity in response.Entities)
    {
        AccountData ad = new AccountData(entity);
        result.Add(ad);
    }

    return result;
}
 
And I was able to access the same service url via directly HttpClient call using access token:
protected override async Task OnInitializedAsync()
{
    var config = new DynamicsCrmConfigProvider();

    baseApiUrl = config.Resource;

    // Get the HttpClient
    _httpClient = HttpClientFactory.CreateClient();

    
    try
    {
        authorizationToken = await TokenAcquisitionService.GetAccessTokenForUserAsync(new string[] { $"{baseApiUrl}user_impersonation" });
    }
    // Microsoft Identity Web specific exception class for use in Blazor or Razor pages to process the user challenge.
    // Handles the MsalUiRequiredException.
    catch (MicrosoftIdentityWebChallengeUserException ex)
    {
        ConsentHandler.HandleException(ex);
    }
    catch (Exception)
    {
        throw new Exception("Error getting access token.");
    }

    // Set the auth token
    _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorizationToken);

    var url = $"{baseApiUrl}api/data/v9.2/accounts({accountId})";

    HttpResponseMessage response = await _httpClient.GetAsync(url);


    if (response.IsSuccessStatusCode)
    {
        string content = await response.Content.ReadAsStringAsync();  
        
    }
    else
    {
        throw new Exception("Error sending request.");
    }
}
 
I browsed the same questions for multiple times and multiple places but have not come across a satisfactory answer thus far. 
 
What was I missing? I have valid Azure App registration. 
 
 
 
I have the same question (0)
  • Alex.P Profile Picture
    68 on at
    Hey there, any luck solving?
    I have the same error for my multitenant app. It works fine with a lot of instances, but fails for one of the customers.

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

#2
Gerardo Rentería García Profile Picture

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

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans