OpenIdDict on Azure App Service
New applications built on ABP.IO rely on OpenIdDict as the authentication server. You might face an issue where you will need to provide a certificate to secure encryption and signing. However, it's not so simple to import/manage an external certificate on an Azure App Service.
Recommended solution is to use KeyVault Certificates and load it at startup of your application. Even though you could use a local certificate for development, the file is not GITted by default, so we would recommend using the Key Vault certificate for local development as well.
- Creating a self-signed certificate in KeyVault.
-411cb0c5f0fab88ca9170eeda189e09e.avif)
- Configure your resources to have access to read the certificate in KeyVault by giving them the roles "Key Vault Certificate User" and "Key Vault Secrets User"
-b3df15968117ec707373f95e8442e674.png)
- Modify your AppService to add an environment variable. Failure to do so might give you an exception when you try to recreate the X509Certificate from the Key Vault object.
WEBSITE_LOAD_USER_PROFILE=1
-5b9cea8bd25c9955834595ee4a99b397.avif)
- Modify the startup code (in your AuthServer project or in your main Host project if the AuthServer has been put inside the same project).
You may want to change your PreConfigureServices method to use the Async version instead.
public override async Task PreConfigureServicesAsync(ServiceConfigurationContext context)
Then apply this code:
//Deactivating the local developement certificates that is enabled by default
PreConfigure<AbpOpenIddictAspNetCoreOptions>(options =>
{
options.AddDevelopmentEncryptionAndSigningCertificate = false;
});
var keyVaultUri = configuration["Settings:KeyVaultUri"];
var certificateClient = new CertificateClient(vaultUri: new Uri(keyVaultUri), credential:
new ChainedTokenCredential(
new AzureCliCredential(),
new ManagedIdentityCredential(),
new DefaultAzureCredential()
));
var secretClient = new SecretClient(vaultUri: new Uri(keyVaultUri), credential:
new ChainedTokenCredential(
new AzureCliCredential(),
new ManagedIdentityCredential(),
new DefaultAzureCredential()
));
var certResponse = await certificateClient.GetCertificateAsync("openiddict");
KeyVaultSecretIdentifier identifier = new KeyVaultSecretIdentifier(certResponse.Value.SecretId);
var secretResponse = await secretClient.GetSecretAsync(identifier.Name, identifier.Version);
KeyVaultSecret secret = secretResponse.Value;
byte[] privateKeyBytes = Convert.FromBase64String(secret.Value);
var cert = new X509Certificate2(privateKeyBytes);
PreConfigure<OpenIddictServerBuilder>(serverBuilder =>
{
serverBuilder.AddSigningCertificate(cert);
serverBuilder.AddEncryptionCertificate(cert);
});
Key Vault will take care of rotating the certificate and your app should be restarted in some way to load the new certificate.