I’m trying to remove a certificate to a service account.
I’m doing this with Microsoft.Graph (5.14.0) with a Service Principal account which is global administrator and has the necessary permissions with Graph.
This master account authenticates via a certificate.
I followed the C# examples like MicrosoftIdentityPlatformProofTokenGenerator
here:
- https://learn.microsoft.com/en-us/graph/api/serviceprincipal-removekey?view=graph-rest-1.0&tabs=csharp
- https://learn.microsoft.com/en-us/graph/application-rollkey-prooftoken?tabs=csharp
I systematically get an Access Token missing or malformed
error… indicating that the problem must surely come from the proof field
var requestBody = new Microsoft.Graph.ServicePrincipals.Item.RemoveKey.RemoveKeyPostRequestBody
{
KeyId = key.KeyId,
Proof = proof
};
try
{
await graphClient.ServicePrincipals[sp.Id].RemoveKey.PostAsync(requestBody);
}
catch (ODataError err)
{
var message = err.Error?.Message;
}
From what I understood:
- We create the proof with the object id of the application associated with the principalservice that makes the call (I also tried with the id of the sp).
- We use the certificate of the principalservice to generate a proof token (I use the same certificate as the one with which I identify myself.. and I also tried with the one I want to delete and the identity of the sp but Nothing)
- Or use Microsoft Graph id as audience (it’s not very clear but I tried with AAD Graph Api as well)
00000002-0000-0000-c000-000000000000
AAD Graph API00000003-0000-0000-c000-000000000000
Microsoft Graph API
- I send the pfxFile (private and public key) to generate the proof
What am I doing wrong?
Thanks in advance
2
Answers
Thanks a lot for your answer!
With your example it seems to me that my token is not correctly created.
I generate the jwt using the example provided on the documentation [https://learn.microsoft.com/en-us/graph/application-rollkey-prooftoken?tabs=csharp][1]
1. Application/ServicePrincipal
When i read your example you POST with postman on
/applications/{id}/removeKey
but the c# graph api...Remove a KeyCredentials of servicePrincipal here
/servicePrincipals/{id}/removeKey
2. JWT contents
The M$ example doesn't talk about the roles to be specified in the token or the possibility of putting
https://graph.microsoft.com
inaud
When trying I still get the same error.. My payload :
I illustrate more the context :
f8d5d85a-...
) is a ServicePrincipal (of APP_1) isGlobal administrator
in my Azure AD and have all GraphApi access with consentsWorkflow
Test the proof jwt sended in
POST
is generated with :-> I test also with aud=00000003-0000-0000-c000-000000000000 and 00000002-0000-0000-c000-000000000000
-> I test with and without roles
-> All graph api autorisation was created in APP_1 and APP_2
:'(
I generated the access token and when tried to
removeKey
, I got the error like below:The error "Access Token missing or malformed" usually occurs if you are passing invalid access token to perform the action or the aud is not matching the access resource.
To resolve the error, check the below:
=
‘ padding character is present in the JWT header or payload, if yes then remove it in order to avoid theAuthentication_MissingOrMalformed
error.Make sure to grant Admin consent for the
Application.ReadWrite.OwnedBy
like below:Decode the token and make sure that the aud is
00000003-0000-0000-c000-000000000000
orhttps://graph.microsoft.com
and required roles are present:In the above code, replace the
servicePrincipal-id
by the below value:Make sure the access token is not expired or test in Postman using the same token.
After the above changes, I am able to removeKey successfully via Postman like below: