In this article I will present my approach in accessing AAD user information using WebApi and a self signed certificate stored in keyvault. I am using .net 5, c#9 and for the test environmentI created an account using https://developer.microsoft.com/en-us/microsoft-365/dev-program , with this account you have a list of sample users which you can use to test your application.
Step 1 - Create a certificate
I am not using a real production ready certificate, and please don’t use this approach in production. I am creating a certificate for development purposes. For this I am using powershell(my version is 7).
This certificate was created and saved in the certificate store. Now we need to get it, we will locate and export it as a pfx file. In the export include also the private key, we will transform the certificate later.
To export the certificate, right click on it and under “all tasks” click “export”.
Great, now the pfx certificate is exported:
Step 2 - Extract the public certificate
To get the public key from the pfx file we can use power shell, I have to admit that I got stuck on the documentation and lost my patience. I used a more friendly (at least for me) library for this, openssl. To install openssl I used choco.
https://chocolatey.org/install
With choco you install openssl using the command : > choco install openssl
To extract the pem file, navigate to the folder where the certificate is stored and type:
Step 3 - AAD App Registration
Navigate to portal.azure.com and login with your test account credentials.
Under AAD we have to register our WebApi and provide it some permissions and upload the certificate public key.
In the registered app, under certificates and secrets upload the pem file
Now for my app I added some application (not delegated) permissions. Don’t forget to click the button “grant admin consent for”
Step 4 - KeyVault
Now we need to upload our certificate in the keyvault (in the same subscription).
For the keyvault the test account is not enough, I activated the free subscription (which provides 200$ free credits for first time use)
In the keyvault, under certificate we upload our certificate
In our example we access KV in 2 ways. By using the DefaultAzureCredentials when the app is deployed, and by using the AzureCliCredentials when in debug.
For the app to be allowed to use the KeyVault, we must provide it some permissions.
In KeyVault, under access policies add a new policy and for the principal select the app



















