Thanks to @Harshitha for pointing me in the right path, using connected services in VS 2019 to connect to a keyVault which can then ref values using appSettings.
To test this I created a new dummy app using a .Net 4.8 framwork application in C#
I have followed this clip:
https://www.youtube.com/watch?v=S7EPrlpPqXw
Basically, use connected services to connect to your key vault.
This will include the following in your web.config file:
< section name= "configBuilders" type= "System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges= "false" requirePermission= "false" / >
< add name= "AzureKeyVault" vaultName= "RealKeyVaultName" type= "Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" / >
< appSettings configBuilders= "AzureKeyVault" >
< !-- Value added by me -- >
< add key= "secretInKV" value= "dummyValue" / >
<code> <configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="AzureKeyVault" vaultName="RealKeyVaultName" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<appSettings configBuilders="AzureKeyVault">
<!-- Value added by me -->
<add key="secretInKV" value="dummyValue" />
</appSettings>
</configuration>
</code>
<configuration>
<configSections>
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
</configSections>
<configBuilders>
<builders>
<add name="AzureKeyVault" vaultName="RealKeyVaultName" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<appSettings configBuilders="AzureKeyVault">
<!-- Value added by me -->
<add key="secretInKV" value="dummyValue" />
</appSettings>
</configuration>
So basically creating a connection to KV using configSection and configBuilders
In code I can then say
< code > var secretValue = ConfigurationManager. AppSettings [ "secretInKV" ] ;
<code>var secretValue = ConfigurationManager.AppSettings["secretInKV"];
</code>
var secretValue = ConfigurationManager.AppSettings["secretInKV"];
and this correctly returns the value stored in my KV, (not dummyValue from the above app settings) which is all working fine.
However when I try to add this to my real application I get an error loading:
Parser Error Message: The configBuilder ‘AzureKeyVault’ failed while processing the configuration section ‘appSettings’.: Error in Configuration Builder ‘AzureKeyVault’::GetValue(secretInKV)
The stack trace errors show:
< code >[ SocketException ( 0x2746 ) : An existing connection was forcibly closed by the remote host ]
[ IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ]
[ WebException: The underlying connection was closed: An unexpected error occurred on a send. ]
[ RequestFailedException: The underlying connection was closed: An unexpected error occurred on a send. ]
[ AggregateException: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions. Retry or by configuring a custom retry policy in ClientOptions. RetryPolicy . ]
[ Exception: Error in Configuration Builder 'AzureKeyVault' :: GetValue ( secretInKV )]
<code>[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.]
[WebException: The underlying connection was closed: An unexpected error occurred on a send.]
[RequestFailedException: The underlying connection was closed: An unexpected error occurred on a send.]
[AggregateException: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy.]
[Exception: Error in Configuration Builder 'AzureKeyVault'::GetValue(secretInKV)]
</code>
[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.]
[WebException: The underlying connection was closed: An unexpected error occurred on a send.]
[RequestFailedException: The underlying connection was closed: An unexpected error occurred on a send.]
[AggregateException: Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy.]
[Exception: Error in Configuration Builder 'AzureKeyVault'::GetValue(secretInKV)]
In code I can say:
var client = new SecretClient(new Uri(keyVaultURL), new DefaultAzureCredential());
var secret = client.GetSecret(secretInKV);
and as all of the depenedencies where added when using the connected service I am able to retrieve the value from KV, but I want to get it from app settings
if I remove
< code > configBuilders= "AzureKeyVault"
<code>configBuilders="AzureKeyVault"
</code>
configBuilders="AzureKeyVault"
from
< code > < appSettings configBuilders= "AzureKeyVault" >
<code> <appSettings configBuilders="AzureKeyVault">
</code>
<appSettings configBuilders="AzureKeyVault">
the application loads, why is this causing an issue please?
I have read similar posts online but was not able to solve,
I am properly connected else I wouldnt be able to get the value with the above mentioned C# code, so why is this causing an issue please?
thank you for any replies
I have matched the Nuget packages in the new dummy app I have created against my actual application and still this issue is happening, I have attached my packages:
< package id= "Azure.Core" version= "1.38.0" targetFramework= "net48" / >
< package id= "Azure.Identity" version= "1.10.3" targetFramework= "net48" / >
< package id= "Azure.Security.KeyVault.Keys" version= "4.0.0" targetFramework= "net48" / >
< package id= "Azure.Security.KeyVault.Secrets" version= "4.0.0" targetFramework= "net48" / >
< package id= "Azure.Storage.Blobs" version= "12.18.0" targetFramework= "net48" / >
< package id= "Azure.Storage.Common" version= "12.17.0" targetFramework= "net48" / >
< package id= "EntityFramework" version= "6.2.0" targetFramework= "net48" / >
< package id= "Microsoft.ApplicationInsights" version= "2.22.0" targetFramework= "net48" / >
< package id= "Microsoft.ApplicationInsights.NLogTarget" version= "2.22.0" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.Cors" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.WebApi" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.WebApi.Client" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.WebApi.Core" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.WebApi.Cors" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.AspNet.WebApi.WebHost" version= "5.2.6" targetFramework= "net48" / >
< package id= "Microsoft.Bcl.AsyncInterfaces" version= "7.0.0" targetFramework= "net48" / >
< package id= "Microsoft.Configuration.ConfigurationBuilders.Azure" version= "2.0.0" targetFramework= "net48" / >
< package id= "Microsoft.Configuration.ConfigurationBuilders.Base" version= "2.0.0" targetFramework= "net48" / >
< package id= "Microsoft.CrmSdk.CoreAssemblies" version= "9.0.2.25" targetFramework= "net48" / >
< package id= "Microsoft.CrmSdk.Deployment" version= "9.0.2.25" targetFramework= "net48" / >
< package id= "Microsoft.CrmSdk.Workflow" version= "9.0.2.25" targetFramework= "net48" / >
< package id= "Microsoft.CrmSdk.XrmTooling.CoreAssembly" version= "9.1.0.79" targetFramework= "net48" / >
< package id= "Microsoft.Identity.Client" version= "4.60.3" targetFramework= "net48" / >
< package id= "Microsoft.Identity.Client.Extensions.Msal" version= "4.60.3" targetFramework= "net48" / >
< package id= "Microsoft.IdentityModel.Abstractions" version= "6.35.0" targetFramework= "net48" / >
< package id= "Microsoft.IdentityModel.Clients.ActiveDirectory" version= "3.19.8" targetFramework= "net48" / >
< package id= "Microsoft.Owin" version= "4.0.1" targetFramework= "net461" / >
< package id= "Microsoft.Owin.Cors" version= "4.0.1" targetFramework= "net461" / >
< package id= "Microsoft.Owin.Host.SystemWeb" version= "4.0.1" targetFramework= "net461" / >
< package id= "Microsoft.Owin.Security" version= "4.0.1" targetFramework= "net461" / >
< package id= "Microsoft.Owin.Security.OAuth" version= "4.0.1" targetFramework= "net461" / >
< package id= "Microsoft.Rest.ClientRuntime" version= "2.3.20" targetFramework= "net48" / >
< package id= "Newtonsoft.Json" version= "11.0.2" targetFramework= "net48" / >
< package id= "NLog" version= "4.7.15" targetFramework= "net48" / >
< package id= "NLog.Config" version= "4.5.9" targetFramework= "net48" / >
< package id= "NLog.Extended" version= "4.5.9" targetFramework= "net48" / >
< package id= "NLog.Extensions.AzureBlobStorage" version= "4.3.1" targetFramework= "net48" / >
< package id= "NLog.Schema" version= "4.5.9" targetFramework= "net48" / >
< package id= "NLog.Web" version= "4.6.0" targetFramework= "net48" / >
< package id= "Owin" version= "1.0" targetFramework= "net461" / >
< package id= "System.Buffers" version= "4.5.1" targetFramework= "net48" / >
< package id= "System.ClientModel" version= "1.0.0" targetFramework= "net48" / >
< package id= "System.Diagnostics.DiagnosticSource" version= "6.0.1" targetFramework= "net48" / >
< package id= "System.IO.FileSystem.AccessControl" version= "5.0.0" targetFramework= "net48" / >
< package id= "System.IO.Hashing" version= "6.0.0" targetFramework= "net48" / >
< package id= "System.Memory" version= "4.5.4" targetFramework= "net48" / >
< package id= "System.Memory.Data" version= "1.0.2" targetFramework= "net48" / >
< package id= "System.Numerics.Vectors" version= "4.5.0" targetFramework= "net48" / >
< package id= "System.Runtime.CompilerServices.Unsafe" version= "6.0.0" targetFramework= "net48" / >
< package id= "System.Security.AccessControl" version= "5.0.0" targetFramework= "net48" / >
< package id= "System.Security.Cryptography.ProtectedData" version= "4.7.0" targetFramework= "net48" / >
< package id= "System.Security.Principal.Windows" version= "5.0.0" targetFramework= "net48" / >
< package id= "System.Text.Encodings.Web" version= "4.7.2" targetFramework= "net48" / >
< package id= "System.Text.Json" version= "4.7.2" targetFramework= "net48" / >
< package id= "System.Threading.Tasks.Extensions" version= "4.5.4" targetFramework= "net48" / >
< package id= "System.ValueTuple" version= "4.5.0" targetFramework= "net48" / >
<code> <packages>
<package id="Azure.Core" version="1.38.0" targetFramework="net48" />
<package id="Azure.Identity" version="1.10.3" targetFramework="net48" />
<package id="Azure.Security.KeyVault.Keys" version="4.0.0" targetFramework="net48" />
<package id="Azure.Security.KeyVault.Secrets" version="4.0.0" targetFramework="net48" />
<package id="Azure.Storage.Blobs" version="12.18.0" targetFramework="net48" />
<package id="Azure.Storage.Common" version="12.17.0" targetFramework="net48" />
<package id="EntityFramework" version="6.2.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.NLogTarget" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.AspNet.Cors" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Azure" version="2.0.0" targetFramework="net48" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Base" version="2.0.0" targetFramework="net48" />
<package id="Microsoft.CrmSdk.CoreAssemblies" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.79" targetFramework="net48" />
<package id="Microsoft.Identity.Client" version="4.60.3" targetFramework="net48" />
<package id="Microsoft.Identity.Client.Extensions.Msal" version="4.60.3" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Abstractions" version="6.35.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.8" targetFramework="net48" />
<package id="Microsoft.Owin" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Cors" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Security" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Security.OAuth" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.20" targetFramework="net48" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net48" />
<package id="NLog" version="4.7.15" targetFramework="net48" />
<package id="NLog.Config" version="4.5.9" targetFramework="net48" />
<package id="NLog.Extended" version="4.5.9" targetFramework="net48" />
<package id="NLog.Extensions.AzureBlobStorage" version="4.3.1" targetFramework="net48" />
<package id="NLog.Schema" version="4.5.9" targetFramework="net48" />
<package id="NLog.Web" version="4.6.0" targetFramework="net48" />
<package id="Owin" version="1.0" targetFramework="net461" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.ClientModel" version="1.0.0" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="6.0.1" targetFramework="net48" />
<package id="System.IO.FileSystem.AccessControl" version="5.0.0" targetFramework="net48" />
<package id="System.IO.Hashing" version="6.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Memory.Data" version="1.0.2" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Security.AccessControl" version="5.0.0" targetFramework="net48" />
<package id="System.Security.Cryptography.ProtectedData" version="4.7.0" targetFramework="net48" />
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="4.7.2" targetFramework="net48" />
<package id="System.Text.Json" version="4.7.2" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>
</code>
<packages>
<package id="Azure.Core" version="1.38.0" targetFramework="net48" />
<package id="Azure.Identity" version="1.10.3" targetFramework="net48" />
<package id="Azure.Security.KeyVault.Keys" version="4.0.0" targetFramework="net48" />
<package id="Azure.Security.KeyVault.Secrets" version="4.0.0" targetFramework="net48" />
<package id="Azure.Storage.Blobs" version="12.18.0" targetFramework="net48" />
<package id="Azure.Storage.Common" version="12.17.0" targetFramework="net48" />
<package id="EntityFramework" version="6.2.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.ApplicationInsights.NLogTarget" version="2.22.0" targetFramework="net48" />
<package id="Microsoft.AspNet.Cors" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.6" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Azure" version="2.0.0" targetFramework="net48" />
<package id="Microsoft.Configuration.ConfigurationBuilders.Base" version="2.0.0" targetFramework="net48" />
<package id="Microsoft.CrmSdk.CoreAssemblies" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.25" targetFramework="net48" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.79" targetFramework="net48" />
<package id="Microsoft.Identity.Client" version="4.60.3" targetFramework="net48" />
<package id="Microsoft.Identity.Client.Extensions.Msal" version="4.60.3" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Abstractions" version="6.35.0" targetFramework="net48" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.8" targetFramework="net48" />
<package id="Microsoft.Owin" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Cors" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Security" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Security.OAuth" version="4.0.1" targetFramework="net461" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.20" targetFramework="net48" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net48" />
<package id="NLog" version="4.7.15" targetFramework="net48" />
<package id="NLog.Config" version="4.5.9" targetFramework="net48" />
<package id="NLog.Extended" version="4.5.9" targetFramework="net48" />
<package id="NLog.Extensions.AzureBlobStorage" version="4.3.1" targetFramework="net48" />
<package id="NLog.Schema" version="4.5.9" targetFramework="net48" />
<package id="NLog.Web" version="4.6.0" targetFramework="net48" />
<package id="Owin" version="1.0" targetFramework="net461" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.ClientModel" version="1.0.0" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="6.0.1" targetFramework="net48" />
<package id="System.IO.FileSystem.AccessControl" version="5.0.0" targetFramework="net48" />
<package id="System.IO.Hashing" version="6.0.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
<package id="System.Memory.Data" version="1.0.2" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Security.AccessControl" version="5.0.0" targetFramework="net48" />
<package id="System.Security.Cryptography.ProtectedData" version="4.7.0" targetFramework="net48" />
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="4.7.2" targetFramework="net48" />
<package id="System.Text.Json" version="4.7.2" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>