Share via

I am accessing API in dyanmics 365 BC using asp.net core 8 token issue

BC Dev 0 Reputation points
2026-03-31T09:28:02.5433333+00:00

When I am accessing the API in Dynamics 365 Business Central using ASP.NET Core 8, but I am facing a token issue

this is my code for token Generate

private async Task<string> GetAccessToken()

{

using var client = new HttpClient();

var url = "https://login.microsoftonline.com/7xxxxxx9/oauth2/v2.0/token";

var body = new Dictionary<string, string>

{

    { "grant_type", "client_credentials" },

    { "client_id", "a3xxxxxx5" },

    { "client_secret", "BFNxxxxxx~" }, // 🔒 move to config later

    { "scope", "https://api.businesscentral.dynamics.com/.default" }

};

var content = new FormUrlEncodedContent(body);

var response = await client.PostAsync(url, content);

var json = await response.Content.ReadAsStringAsync();

var tokenObj = JsonSerializer.Deserialize<TokenResponse>(json);

return tokenObj.access_token;
```}

public class TokenResponse

{

```sql
public string access_token { get; set; }
```}

Microsoft Security | Microsoft Entra | Microsoft Entra ID

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.