Search code examples
c#azure-managed-identity

Managed Identity - how to debug locally


I am using the following code to test system managed identity on my web app and it works fine when I deploy in Azure but is there a way to test locally(without giving permissions to my Azure account for the resource?)?

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();

KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = await keyVaultClient.GetSecretAsync("https://EASDemo.vault.azure.net/secrets/test")
                .ConfigureAwait(false);
return new string[] { secret.Value };

Solution

  • Options to test locally (VS, CLI) are documented here:

    Authenticating with Visual Studio

    To authenticate by using Visual Studio:

    1. Sign in to Visual Studio and use Tools > Options to open Options.
    2. Select Azure Service Authentication, choose an account for local development, and select OK.

    If you run into problems using Visual Studio, such as errors that involve the token provider file, carefully review the preceding steps.

    You may need to reauthenticate your developer token. To do so, select Tools > Options, and then select Azure Service Authentication. Look for a Re-authenticate link under the selected account. Select it to authenticate.

    If you do not want to use your developer identity, you can also use a certificate or secret key (though not recommended as it can be checked in to source repository by mistake). These options are documented here.