C
C#3w ago
WAASUL

Azure Managed Identity

if (!builder.Environment.IsProduction()) return;

// Retrieve configuration settings
var configuration = builder.Configuration;

try
{
// Add Azure Key Vault
var keyVaultEndpoint = new Uri(configuration[Configs.KeyVaultUrl]!);
var azureCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
ManagedIdentityClientId = Configs.ManagedIdentityClientId,
});
var secretClient = new SecretClient(keyVaultEndpoint, azureCredential);
configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
}
catch (Exception e)
{
Log.Error(e.Message);
}
if (!builder.Environment.IsProduction()) return;

// Retrieve configuration settings
var configuration = builder.Configuration;

try
{
// Add Azure Key Vault
var keyVaultEndpoint = new Uri(configuration[Configs.KeyVaultUrl]!);
var azureCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions
{
ManagedIdentityClientId = Configs.ManagedIdentityClientId,
});
var secretClient = new SecretClient(keyVaultEndpoint, azureCredential);
configuration.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
}
catch (Exception e)
{
Log.Error(e.Message);
}
I have spent almost 8 hours trying to find the issue. For some reason managed identity is not working in production mode. It seems to be unable to load the secrets. Even though I have successfully defined the resource access polices. The code was working fine before I went to sleep. All of a sudden when I woke up its not working? Can anyone please help?
5 Replies
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3w ago
That's the weird part. I had it just like that. Everything was working fine. The next day, for some reason it was unable to load the secrets in production mode. I have looked at the polices and made sure that everything is connected. Still for some reason its unable to load them in production mode. When running on my local machine it's able to load them. I'm running on an linux appservice. Currently when taking a look at the log stream. I'm getting
2024-12-04T14:45:06.8965555Z at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
2024-12-04T14:45:06.8965576Z --- End of stack trace from previous location ---
2024-12-04T14:45:06.9048913Z at System.Lazy`1.CreateValue()
2024-12-04T14:45:06.9049108Z at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd[TArg](String name, Func`3 createOptions, TArg factoryArgument)
2024-12-04T14:45:06.9049138Z at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
2024-12-04T14:45:06.9049198Z at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
2024-12-04T14:45:06.9049223Z at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
2024-12-04T14:45:06.9049249Z at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2024-12-04T14:45:06.9049273Z at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
2024-12-04T14:45:06.8965555Z at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
2024-12-04T14:45:06.8965576Z --- End of stack trace from previous location ---
2024-12-04T14:45:06.9048913Z at System.Lazy`1.CreateValue()
2024-12-04T14:45:06.9049108Z at Microsoft.Extensions.Options.OptionsCache`1.GetOrAdd[TArg](String name, Func`3 createOptions, TArg factoryArgument)
2024-12-04T14:45:06.9049138Z at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.InitializeAsync(AuthenticationScheme scheme, HttpContext context)
2024-12-04T14:45:06.9049198Z at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
2024-12-04T14:45:06.9049223Z at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
2024-12-04T14:45:06.9049249Z at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
2024-12-04T14:45:06.9049273Z at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
WAASUL
WAASULOP3w ago
@TeBeCo I will definitely take a look into that. Right now, it seems to be working again. It's errors like this that I don't like. Because I didn't really found out what the issue was. Is there a way to capture issues like this?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server