How do I resolve the error AADSTS7000218: The request body must contain the following parameter: 'client_secret' or 'client_assertion'

Viewed 29842

This is how I have written code and trying to get the output.

The request body must contain the following parameter: client_secret or client_assertion

 static async Task<AuthenticationResult> getAccessToken()
 {
     string hardcodedUsername = "";
     string hardcodedPassword = "";
     string tenantName = "projectwidgets.com";
     string authString = "https://login.microsoftonline.com/" + tenantName;
     AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
     //Config for OAuth client credentials
     string clientId = "as";
     string key = "kk";
     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenantName);
     var authContext = new AuthenticationContext(authority);
     AuthenticationResult result = null;
     try
     {
         result = await authContext.AcquireTokenAsync("https://pwsnapitazure.azurewebsites.net", clientId, new UserPasswordCredential(hardcodedUsername, hardcodedPassword));
     }
     catch (Exception ex)
     {
          Console.WriteLine(ex.StackTrace);
          System.Diagnostics.Debug.WriteLine(ex.Message);
     }                        
     return result;
 }
3 Answers

As the Azure App Registration UI has changed from legacy authentication, you will need to enable an additional setting called "treat application as a public client". Under Default Client Type, set this setting to Yes:

screenshot of AAD App Registration showing "Treat application as a public client" set to "yes" under the 'Default client type' subsection of the 'Advanced Settings' section

In the Manifest also you can control this by setting:

"allowPublicClient": true

This is now found under Azure Active Directory -> App registrations -> <YOUR-APP> -> Authentication -> Advanced settings -> Allow public client flows

enter image description here

You can however receive this error even if Allow public client flows is set to yes.

AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

Under Authentication look at Platform configurations:

This App registration only has Web platform enalbed.

enter image description here

When trying to use publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync(); I got the following error in a console app:

Microsoft.Identity.Client.MsalServiceException: 'A configuration issue is preventing authentication - check the error message from the server for details. You can modify the configuration in the application registration portal. See https://aka.ms/msal-net-invalid-client for details. Original exception: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.

enter image description here

If I then switched to Mobile and desktop applications platform everything stared working using the same Redirect URIs.

enter image description here

Related