Specify Key Vault URI in an AppSettin? - azure-keyvault

I have multiple instances of a web app (Azure App Services), they're all the same code, but are connected to client specific DBs. So they're all deployed with the same web.config, but they have different connection strings and app settings, which I'm currently storing in the App Service's Configuration blade.
I want to move to using Key Vault instead. I have it working on a test app, configured in the web.config like this:
<add name="AzureKeyVault" vaultName="my-vault" vaultUri="https://my-vault.vault.azure.net" type="Microsoft.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Azure, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /></builders>
However, I realise I'll need a Key Vault per instance of the app, yet each app's web config will then need to be different to hold the relevant vaultUri.
So my question is: is it possible to store the vaultUri in an AppSetting (stored in the App's Configuration blade) and somehow reference that in the configBuilder line, for example, something like this:
<add name="AzureKeyVault" vaultName="my-vault" vaultUri="[AppSetting.VaultName]" ...
Or, is there another way to achieve what I'm try to do - essentially reference a different Key Vault without requiring a different web.config for each app instance

If I understand this correctly, you're asking how to get the key vault info from configuration blade -> web.config -> configbuilder code
If that's accurate, then I think you're overthinking this. Go straight from configuaration blade -> configbuilder code. Skip the web.config altogether.

Based on my research you can add the keyvault name inside appsettings.json Not the URI
example:
The use the provider, add a reference to the Microsoft.Extensions.Configuration.AzureKeyVault package and add the following configuration to the startup code in Program.cs.
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var root = config.Build();
config.AddAzureKeyVault(
$”https://{root["KeyVault:Vault"]}.vault.azure.net/",
root[“KeyVault:ClientId”],
root[“KeyVault:ClientSecret”]);
})
.UseStartup<Startup>();
For more details refer this document
And Also check with the Configuration builders provide a modern and agile mechanism for ASP.NET apps to get configuration values from external sources.
Configuration builders used to improve protection of configuration data by drawing from sources previously unavailable (for example, Azure Key Vault and environment variables) in the .NET configuration system

Related

WCF Service Library Configuration (App.config) And Webapplication Config(Web.config) Issue

I have a Service Client Library project which has its own
app.config
.
This project is referenced in my Web Application. The problem is My web application throws following exception
Could not find endpoint element with name 'HttpEndPoint' and contract 'ServiceLibReference1.IDalService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.
There is nothing wrong with the service. If i directly refer my service in the web application it works fine as client configurations get added to the web.config. But my requirement is to keep it in a separate library project.
The reason for the exception is service configurations are not added to the web.config. Should i always add them manually? Should not VS add corresponding configurations into web.config whenever a service library is referenced? or am i missing something ?
thanks
Service libraries are, by design, intended to be used by (potentially) multiple different applications. Therefore, they don't use the app.config file included in the template - they will use the config file (web/app) of the application that is referencing them. This promotes code reuse - if you have certain methods that you are always using across multiple applications, it's easier to put them in a separate class library assembly. One benefit of that design is that you only need to change the code in one place to make the change effective for all using applications.
So yes, you will always need to add the service configuration to the specific application configuration file. Unfortunately, VS does not know to do this.

deploy asp.net solution to multiple hosting systems

I am working on an asp.net solution and using my remote testing environment with godaddy.
The application is ready for deployment so i have two questions:
1- since the database will be different , i am thinking about using a different Web.config , but not sure how does it work and how to implement that so i can choose easily which web config depending on the poriject/solution
2- the solution i am working on could be personalized and deployed to multiple clients , so each client version will be different (Like the logo and other stuff not the design and functionality), do i need to create separate solution for each client or should i create a separate website project instead?
what's best practices in this case
Use configurations and associated config transforms. By default, your project gets a Debug and Release configuration, but you can add additional configurations. Then, for each configuration, you can have a separate Web.config transform, Web.Server1.config, Web.Server2.config, Web.Client1.config, Web.Client2.config, etc. When publishing, you choose which configuration you want to publish with, and that associated transform will be run against the Web.config file to change out connection strings, app settings, whatever.
For more information see: How to: Transform Web.config When Deploying a Web Application Project | MSDN

Accessing ASP.NET MVC Variables in Elastic Beanstalk Web.config

I'm trying to update my elastic beanstalk configuration with custom web.config keys for our production servers, passwords, etc.
According to these .NET docs, I can use ConfigurationManager.AppSettings to access these variables. I have some defaults that are in there for my local machine, and these are what get read, instead of the overrides in the web UI.
Specify up to five additional key-value pairs by entering them in the
PARAM boxes.
You might have a code snippet that looks similar to the following to
access the keys and parameters:
NameValueCollection appConfig = ConfigurationManager.AppSettings;
string param1 = appConfig["PARAM1"];
How do I access my web.config overrides in Elastic Beanstalk?
It turns out that the configuration variables will only be added if they do not previously exist in web.config. This is a different behavior than I have experienced in Azure, where parameters would override web.config.
You can validate this by RDP'ing into an EC2 instance, and viewing web.config. New parameters will be added, but ones that exist in web.config will be ignored.
You can replicate the override behavior by using the xdt "Remove" Transform in Web.Release.Config
<add key="foo" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
Then set the "foo" parameter in Elastic Beanstalk using the web tool, file config, or CLI

Is it possible to consume WCF service from same project?

I have an ASP.NET MVC 3 application that uses a WCF service within the same project. Ideally I'd like to call out to this service using jQuery. However, I cannot seem to wrap my head around what I'm doing. Should I still create an endpoint in the configuration? Right now I receive the following exception:
Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service.
I can enable anonymous authentication for IIS but I'd prefer to use Windows. When I setup a binding configuration, since there is no endpoint, I'm not sure where to add that configuration to.
If you want to be able to reach your WCF service, you will generally need to setup an endpoint. An alternative approach would be to host your service "In-Proc" using the InProcFactory clas, which is part of the ServiceModelEx library from Juval Löwy available from the downloads page of his website (registration is required to download it, just search for "ServiceModelEx" and click the link). That approach would look like:
IMyService proxy = InProcFactory.CreateInstance<MyServiceClass, IMyService>();
proxy.MyMethod();
This reduces the configuration if you don't need to do any custom configuration; however, as soon as you hit a boundary with the default configuration, you'll either need to go back to using a configured endpoint, or looking for a way to programmatically update your service's configuration.
You'll need an endpoint, but as with all WCF endpoints it doesn't necessarily need to be defined in the config file - you're free to define it in code.
As you're already in a web project, your simplest solution will be to host the WCF service in IIS. This works very easily with a config file, and in .NET 4 most of the configuration is defaulted (a lot simpler than 3.5)
Once your service is defined you need to instantiate a channel or a client. You can use the svcutil tool to generate a proxy (using the 'Add New Service Reference...' wizard), or just create a ChannelFactory
var factory = new ChannelFactory<MyService>(typeof(MyService).FullName);
MyService channel = factory.CreateChannel();
// use the channel as you would a normal instance of the class
var result = channel.MyOperation("hello world");
Again, this pattern will retrieve configuration from your web.config file. Because your project is both the service and the client, you'll need both sections. This isn't a complete configuration but should give you the idea...
<system.serviceModel>
<services>
<service name="MyProject.MyService">
<endpoint binding="basicHttpBinding"
contract="MyProject.IMyService" />
</service>
</services>
<client>
<endpoint name="MyProject.MyService"
address="http://localhost"
binding="basicHttpBinding"
contract="MyProject.IMyService" />
</client>
</system.serviceModel>

Entity Framework + SQLite deployment

I have a ASP.NET MVC app that is using SQLite database through Entity Framework.
Everything works on VS 2008's local development webserver.
However, deploying the web app to my service provider causes this error:
[ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.]
System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +1308959
System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +35
Service provider has commented that they do not support SQLite. I had though that SQLite is independent of service provider's settings since it's App_Data deployable.
Has anyone experiences of a succesfull Entity Framework + SQLite deployment?
Cheers,
-pom-
You're unlikely to be reading this anymore, but you're missing the following in your app.config (or, for you, web.config):
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite"
description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
Specifically, if you're using sqlite in a library which is linked into your website, you must add this to the config file of the website - not the library! This is because of how you're loading the provider: essentially, you're determining at runtime which dll to load, using the string "System.Data.SQLite", and locating the appropriate provider is done using the settings of the entry assembly.
Edit: By the way, when you're writing the library that has an sqlite dependancy, you can avoid this complexity. You do not need to use DbProviderFactories to look for sqlite at runtime; you can take a compile-time dependancy just as well, which can be easier to manage. Then you can ignore the above app.config section, and instead replace all instances of:
DbProviderFactories.GetFactory("System.Data.SQLite").CreateConnection()
with
System.Data.SQLite.SQLiteFactory.Instance.CreateConnection()
If you do so, you're using a plain library call to create the connection and there's no runtime selection of db provider. That can be less flexible since you can no longer exchange data providers via the config file, but for many libraries that's sufficient. Unfortunately, if you don't control the library code, this isn't an option.
Have you tried adding the required DLL(s) to your application's bin directory? You might want to look at Phil Haack's article on Bin Deploying ASP.NET MVC for ideas on how to do this automatically.
SQLite needs full trust permission for ASP.NET application deployment. Many shared hosting providers don't allow that. You might wan't to check this.

Resources