Skip to main content

Implementing an OAuth client credentials flow with ADFS–Part 4–Understanding and fixing returned error codes

It looked like most of the world has made the switch to Microsoft Entra(Azure Active Directory). However one of my clients is still using ADFS. Unfortunately there isn't much information left on how to get an OAuth flow up and running in ADFS. Most of the links I found point to documentation that no longer exists. So therefore this short blog series to show you end-to-end how to get an OAuth Client Credentials flow configured in ADFS.

Part 1 - ADFS configuration

Part 2 – Application configuration

Part 3 – Debugging the flow

Part 4 (this post) – Understanding and fixing returned error codes

Last post we updated our configuration so we could see any errors returned and are able to debug the authentication flow. In the first 2 posts I showed you everything that was needed to get up and running. The reality was that it took some trial and error to get everything up and running.

In this post I share all the errors I got along the way and how I fixed them.

IDX10204: Unable to validate issuer

It all started with the following error message:

IDX10204: Unable to validate issuer. validationParameters.ValidIssuer is null or whitespace AND validationParameters.ValidIssuers is null or empty.

In my case the issue was causes by specifying the wrong ADFS URL.

I was using:

options.Authority = "https://<servername>/adfs/oauth";

Instead of:

options.Authority = "https://servername/adfs";

IDX10214: Audience validation failed

After fixing the first error message, I was immediately welcomed by a second error:

IDX10214: Audience validation failed. Audiences: 'urn:microsoft:userinfo'

A search on the Internet brought me to the ADFS documentation, where I found the following:

AD FS identifies the resource that the client wants to access through the resource parameter that's passed in the authentication request. If you use the MSAL client library, the resource parameter isn't sent. Instead, the resource URL is sent as a part of the scope parameter: scope = [resource url]/[scope values, for example, openid].

If the resource isn't passed using the resource or scope parameters, AD FS uses a default resource urn:microsoft:userinfo whose policies, such as, MFA, issuance, or authorization policy, can't be configured.

I originally passed the scope like this:

// Replace these with your OAuth server details

string tokenEndpoint = "https://<servername>/adfs/oauth2/token";

string clientId = "<client id>";

string clientSecret = "<client secret>";

string scope = "readdata";

string apiEndpoint = "https://localhost:7002/weatherforecast/";

var client = new HttpClient();

// Obtain the token

var tokenResponse = await client.PostAsync(tokenEndpoint, new FormUrlEncodedContent(new[]

{

    new KeyValuePair<string, string>("client_id", clientId),

    new KeyValuePair<string, string>("client_secret", clientSecret),

    new KeyValuePair<string, string>("grant_type", "client_credentials"),

    new KeyValuePair<string, string>("scope", scope)

}));

Instead I had to prefix the scope with the resource as specified in the documentation:

// Replace these with your OAuth server details

string tokenEndpoint = "https://<servername>/adfs/oauth2/token";

string clientId = "<client id>";

string clientSecret = "<client secret>";

string scope = "https://localhost/exampleapi/readdata";

string apiEndpoint = "https://localhost:7002/weatherforecast/";

var client = new HttpClient();

// Obtain the token

var tokenResponse = await client.PostAsync(tokenEndpoint, new FormUrlEncodedContent(new[]

{

    new KeyValuePair<string, string>("client_id", clientId),

    new KeyValuePair<string, string>("client_secret", clientSecret),

    new KeyValuePair<string, string>("grant_type", "client_credentials"),

    new KeyValuePair<string, string>("scope", scope)

}));

An alternative solution is to pass the resource parameter as well and leave the scope ‘as-is’:

// Replace these with your OAuth server details

string tokenEndpoint = "https://<servername>/adfs/oauth2/token";

string clientId = "<client id>";

string clientSecret = "<client secret>";

string scope = "readdata";

string resource= "https://localhost/exampleapi";

string apiEndpoint = "https://localhost:7002/weatherforecast/";

var client = new HttpClient();

// Obtain the token

var tokenResponse = await client.PostAsync(tokenEndpoint, new FormUrlEncodedContent(new[]

{

    new KeyValuePair<string, string>("client_id", clientId),

    new KeyValuePair<string, string>("client_secret", clientSecret),

    new KeyValuePair<string, string>("grant_type", "client_credentials"),

    new KeyValuePair<string, string>("scope", scope),

    new KeyValuePair<string, string>("resource", resource)

}));

IDX10205: Issuer validation failed

Not there yet! The next error I got was the following one:

IDX10205: Issuer validation failed. Issuer: 'https://<servername>/adfs/services/trust'. Did not match: validationParameters.ValidIssuer: 'null' or validationParameters.ValidIssuers: 'null' or validationParameters.ConfigurationManager.CurrentConfiguration.Issuer: 'https://<servername>/adfs'. For more details, see https://aka.ms/IdentityModel/issuer-validation.

To fix this error I had to do 2 things, first check that the MetadataAddress was set correctly:

options.MetadataAddress = "https://<servername>/adfs/.well-known/openid-configuration";

Second I had to explicitly specify one or more ValidIssuers:

    options.Authority = "https://<servername>/adfs"; //Replace with your OAuth server URL

    options.Audience = "https://localhost/exampleapi"; // Replace with your API audience

    options.TokenValidationParameters.ValidIssuer = “/adfs/services/trust">http://<servername>/adfs/services/trust”;

HTTP 400 response from ADFS

A last error I got that didn’t result in a specific authentication error but in an 400 response from ADFS was caused by having an invalid scope specified.

Here is the scope I was using:

string scope = "https://localhost/exampleapi/readdata";

The mistake I made is that I originally configured the identifier for the API with a trailing slash.

I was using the following URL:

https://localhost/exampleapi/

After updating the URL to the following(notice that the trailing slash is gone), it finally worked:

https://localhost/exampleapi

More information

ValidatingTokens · AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet Wiki · GitHub

AD FS OpenID Connect/OAuth concepts | Microsoft Learn

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

VS Code Planning mode

After the introduction of Plan mode in Visual Studio , it now also found its way into VS Code. Planning mode, or as I like to call it 'Hannibal mode', extends GitHub Copilot's Agent Mode capabilities to handle larger, multi-step coding tasks with a structured approach. Instead of jumping straight into code generation, Planning mode creates a detailed execution plan. If you want more details, have a look at my previous post . Putting plan mode into action VS Code takes a different approach compared to Visual Studio when using plan mode. Instead of a configuration setting that you can activate but have limited control over, planning is available as a separate chat mode/agent: I like this approach better than how Visual Studio does it as you have explicit control when plan mode is activated. Instead of immediately diving into execution, the plan agent creates a plan and asks some follow up questions: You can further edit the plan by clicking on ‘Open in Editor’: ...