We're transitioning to client certificate authentication in our application and are currently working on retrieving the count of deleted application objects from Microsoft Graph API following https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers#client-credentials-provider. Here's the code we've developed so far:
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "YOUR_CLIENT_ID";
var tenantId = "YOUR_TENANT_ID";
var clientCertificate = new X509Certificate2("MyCertificate.pfx");
var options = new ClientCertificateCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientCertCredential = new ClientCertificateCredential(
tenantId, clientId, clientCertificate, options);
var graphClient = new GraphServiceClient(clientCertCredential, scopes);
try
{
var result = await graphClient.Directory.DeletedItems.GraphApplication.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Count = true;
requestConfiguration.QueryParameters.Orderby = new string[] { "deletedDateTime asc" };
requestConfiguration.QueryParameters.Select = new string[] { "id", "displayName", "deletedDateTime" };
requestConfiguration.Headers.Add("Consistencylevel", "Eventual");
});
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
However, we encounter the following exception when attempting to list deleted applications:
Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException: 'The specified network password is not correct.'
We've ensured that the calling application has been granted the required permissions, including Directory.Read.All and Application.Read.All.
Could you please assist in identifying the issue and providing guidance on how to resolve it? Any help would be greatly appreciated.
In my case, I created certificates with private key using below PowerShell commands:
Response:
Now, I uploaded this certificate in calling app registration that has required permissions like Application.Read.All:
When I ran below modified code by including private key with certificate, I got the response with expected results like this:
Response:
To confirm that, I checked in Portal where the total count of deleted app registrations is same as below: