I have created an ASP.NET Core 6 Web API in C# with Docker support and it worked perfectly when I try to run from Visual Studio. When I deploy the docker build image to docker desktop app, I get an error:
2024-03-29 17:50:53 info: Microsoft.Hosting.Lifetime[14] 2024-03-29 17:50:53 Now listening on: http://[::]:8080 2024-03-29 17:50:53 info: Microsoft.Hosting.Lifetime[0] 2024-03-29 17:50:53 Application started. Press Ctrl+C to shut down. 2024-03-29 17:50:53 info: Microsoft.Hosting.Lifetime[0] 2024-03-29 17:50:53 Hosting environment: Development 2024-03-29 17:50:53 info: Microsoft.Hosting.Lifetime[0] 2024-03-29 17:50:53 Content root path: /app/ 2024-03-29 17:50:59 warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] 2024-03-29 17:50:59 Failed to determine the https port for redirect. 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Getting token Async 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Use Key Vault True 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Keyvault : : XXXXX 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Keyvault URL : : https://XXXXX.vault.azure.net 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Creating Secret client 2024-03-29 17:50:59 info: WebApplication Controllers.HealthController[0] 2024-03-29 17:50:59 Getting Secret 2024-03-29 17:51:02 fail: WebApplication Controllers.HealthController[0] 2024-03-29 17:51:02 Health Controller : InteractiveBrowserCredential authentication failed: Persistence check failed. Inspect inner exception for details 2024-03-29 17:51:02 Azure.Identity.AuthenticationFailedException: InteractiveBrowserCredential authentication failed: Persistence check failed. Inspect inner exception for details 2024-03-29 17:51:02 ---> Microsoft.Identity.Client.Extensions.Msal.MsalCachePersistenceException: Persistence check failed. Inspect inner exception for details 2024-03-29 17:51:02 ---> System.DllNotFoundException: Unable to load shared library 'libsecret-1.so.0' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibsecret-1.so.0: cannot open shared object file: No such file or directory 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.Libsecret.secret_schema_new(String name, Int32 flags, String attribute1, Int32 attribute1Type, String attribute2, Int32 attribute2Type, IntPtr end) 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.LinuxKeyringAccessor.GetLibsecretSchema() 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.LinuxKeyringAccessor.Write(Byte[] data) 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.Storage.VerifyPersistence() 2024-03-29 17:51:02 --- End of inner exception stack trace --- 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.Storage.VerifyPersistence() 2024-03-29 17:51:02 at Microsoft.Identity.Client.Extensions.Msal.MsalCacheHelper.VerifyPersistence() 2024-03-29 17:51:02 at Azure.Identity.MsalCacheHelperWrapper.VerifyPersistence() 2024-03-29 17:51:02 at Azure.Identity.TokenCache.GetCacheHelperAsync(Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.TokenCache.GetCacheHelperAsync(Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.TokenCache.RegisterCache(Boolean async, ITokenCache tokenCache, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.MsalClientBase GetClientAsync(Boolean enableCae, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.MsalPublicClient.AcquireTokenInteractiveCoreAsync(String[] scopes, String claims, Prompt prompt, String loginHint, String tenantId, Boolean enableCae, BrowserCustomizationOptions browserOptions, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.MsalPublicClient.AcquireTokenInteractiveAsync(String[] scopes, String claims, Prompt prompt, String loginHint, String tenantId, Boolean enableCae, BrowserCustomizationOptions browserOptions, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.InteractiveBrowserCredential.GetTokenViaBrowserLoginAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.InteractiveBrowserCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 --- End of inner exception stack trace --- 2024-03-29 17:51:02 at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage, Boolean isCredentialUnavailable) 2024-03-29 17:51:02 at Azure.Identity.InteractiveBrowserCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.InteractiveBrowserCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.DefaultAzureCredential.GetTokenFromSourcesAsync(TokenCredential[] sources, TokenRequestContext requestContext, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.CredentialDiagnosticScope.FailWrapAndThrow(Exception ex, String additionalMessage, Boolean isCredentialUnavailable) 2024-03-29 17:51:02 at Azure.Identity.DefaultAzureCredential.GetTokenImplAsync(Boolean async, TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Identity.DefaultAzureCredential.GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueFromCredentialAsync(TokenRequestContext context, Boolean async, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AccessTokenCache.GetHeaderValueAsync(HttpMessage message, TokenRequestContext context, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.AuthenticateAndAuthorizeRequestAsync(HttpMessage message, TokenRequestContext context) 2024-03-29 17:51:02 at Azure.Security.KeyVault.ChallengeBasedAuthenticationPolicy.AuthorizeRequestOnChallengeAsyncInternal(HttpMessage message, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.BearerTokenAuthenticationPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory 1 pipeline, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.RedirectPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory 1 pipeline, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory 1 pipeline, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.RetryPolicy.ProcessAsync(HttpMessage message, ReadOnlyMemory 1 pipeline, Boolean async) 2024-03-29 17:51:02 at Azure.Core.Pipeline.HttpPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Security.KeyVault.KeyVaultPipeline.SendRequestAsync(Request request, CancellationToken cancellationToken) 2024-03-29 17:51:02 at Azure.Security.KeyVault.KeyVaultPipeline.SendRequestAsync[TResult](RequestMethod method, Func 1 resultFactory, CancellationToken cancellationToken, String[] path) 2024-03-29 17:51:02 at Azure.Security.KeyVault.Secrets.SecretClient.GetSecretAsync(String name, String version, CancellationToken cancellationToken) 2024-03-29 17:51:02 at WebApplication Controllers.AdministratorControllerBase.GetSpnSecretAsync(String secretKey) in /src/WebApplication1/Controllers/AdministratorControllerBase.cs:line 43 2024-03-29 17:51:02 at WebApplication Controllers.AdministratorControllerBase.GetTokenAsync() in /src/WebApplication1/Controllers/AdministratorControllerBase.cs:line 59 2024-03-29 17:51:02 at WebApplication Controllers.HealthController.Get() in /src/WebApplication1/Controllers/HealthController.cs:line 30 2024-03-29 17:51:10 info: WebApplication1 Controllers.WeatherForecastController[0] 2024-03-29 17:51:10 Use Key Vault True 2024-03-29 17:51:10 info: WebApplication Controllers.WeatherForecastController[0] 2024-03-29 17:51:10 Keyvault : : XXXXX 2024-03-29 17:51:10 info: WebApplication Controllers.WeatherForecastController[0] 2024-03-29 17:51:10 Keyvault URL : : https://XXXX.vault.azure.net 2024-03-29 17:51:10 info: WebApplication Controllers.WeatherForecastController[0] 2024-03-29 17:51:10 Creating Secret client 2024-03-29 17:51:10 info: WebApplication Controllers.WeatherForecastController[0] 2024-03-29 17:51:10 Getting Secret 2024-03-29 19:13:04 info: Microsoft.Hosting.Lifetime[0] 2024-03-29 19:13:04 Application is shutting down...
I also tried to run the project with .NET 7 and used the latest packages and followed the checked on the https://github.com/Azure/azure-sdk-for-net/issues/28120
This is my code:
protected static async Task<string> GetSpnSecretAsync(string secretKey)
{
var keyVaultName = Environment.GetEnvironmentVariable("KEYVAULT");
var keyVaultUrl = $"https://{keyVaultName}.vault.azure.net";
var credential = new DefaultAzureCredential(includeInteractiveCredentials: true);
var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential: credential);
var secret = await client.GetSecretAsync(secretKey);
var secretValue = secret.Value.Value;
return secretValue;
}