📜 ⬆️ ⬇️

Non-standard use of Protected Configuration Provider in ASP.NET Web Forms projects

Today I would like to tell you about one interesting, in my opinion, opportunity that can be obtained from the Protected Configuration Provider.

So what is a Protected Configuration Provider? This is a class that allows you to encrypt and decrypt the contents of certain sections of the web.config configuration file. This is usually used when deploying a ready-made project on a hosting to protect confidential information - user names, passwords, database connection strings, encryption keys, etc.

It is registered in the configProtectedData section:

<configuration> <configProtectedData defaultProvider="SampleProvider"> <providers> <add name="SampleProvider" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" keyContainerName="SampleKeys" useMachineContainer="true" /> </providers> </configProtectedData> </configuration> 

')
Used in the appSettings and connectionStrings sections:

 <appSettings configProtectionProvider="SampleProvider"> <EncryptedData> ... </EncryptedData> </appSettings> <connectionStrings configProtectionProvider="SampleProvider"> <EncryptedData> ... </EncryptedData> </connectionStrings> 


Let's try to use the capabilities of this class non-standard, namely, to create branches in the configuration within one project - configuration profiles, depending on the name of the application's virtual directory within one web site.

What is it for? First of all, to deploy test lines of sites based on the same project, but with different strings to connect to the database, and other settings in the appSettings and connectionStrings sections.

In other words, it can greatly simplify life when you need to deploy, for example, several identical sites, only with different settings, but having common code. For example, with different subject names or database connection strings.

We will write and install such a provider.

In IIS Manager, create a site “DemoSite”, which will be linked to an empty folder so far. Next, create two applications (virtual directories) that will refer to the root folder of our project, where our web.config is located.



In the web.config file, change the appSettings section and connectionStrings to make something like this:

 <?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> ... <appSettings configProtectionProvider="DemoConfigurationProvider"> <EncryptedData> <profile1> ... </profile1> <profile2> ... </profile2> <defaultSettings> ... </defaultSettings> </EncryptedData> </appSettings> ... <connectionStrings configProtectionProvider="DemoConfigurationProvider"> <EncryptedData> <profile1> ... </profile1> <profile2> ... </profile2> <defaultSettings> ... </defaultSettings> </EncryptedData> </connectionStrings> ... <configProtectedData> <providers> <add name="DemoConfigurationProvider" type="Renascence.DemoConfigurationProvider,DemoConfigurationProvider" /> </providers> </configProtectedData> </configuration> 


Now we have the profile1 and profile2 sections in the config, and also we have specified the DemoConfigurationProvider class as a provider.

After these manipulations, you can access your site using these profiles. Like this: localhost / DemoSite / profile1 or localhost / DemoSite / profile2 . Corresponding settings from the profile1 and profile2 sections will be applied accordingly.

I hope the idea is clear and will be useful to someone. Now we just need to implement the DemoConfigurationProvider class.

I will not discuss this class in detail in this article; I will simply give a link to the finished demo project, which includes the source code.

After running the demo project you should get something like the following:



Download: demo on github

Source: https://habr.com/ru/post/152521/


All Articles