How to encrypt a file linked to a web.config - asp.net

I have a web.config file that links to a common.config file. common.config is being used by multiple applications. I used the aspnet_regiis.exe, but that only encrypts the web.config file. How can i encrypt the common.config file?
web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="C:\Users\naem\Documents\common.config" />
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
common.config file:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>

I don't have the rep to comment on the original post, but this looks like a duplicate of How to encrypt .config file Asp.net

I found workaround to solve this issue. Hope it helps.
Rename your common.config to web.config temporarily.
Add configuration as root element to this file. So your common.config will look like as follows.
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="myKey" value="This is the Value!!!!"/>
</appSettings>
</configuration>
Run encrypt command.
aspnet_regiis.exe -pef appSettings "C:\Users\naem\Documents"
Open encrypted file and remove Configuration tags.
Rename file to Common.config

Related

Sections must only appear once - appsettings

I'm a bit confused as to how I can specify another .config file in my web.config while retaining parts of the original web config. I want to put my connection strings in another file but when I build the project I get an error about there being multiple appsettings elements.
I have this:
<appSettings file="ConnectionStrings.config">
</appSettings>
then further down, because it's a Crystal Reports application, these settings are specified. I don't want these keys in my connnectionstrings.config file as they're not relevant.
<appSettings>
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
How do I keep my seperate config file and the Crystal settings above, without putting them all in the connectionstrings.config file?
You main configuration file(web.config) should look like this
<?xml version="1.0"?>
<configuration>
<!--other sections-->
<appSettings file="appSettings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>
<!--other sections-->
</configuration>
Further your separate appSettings.config should look like this
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="YourConnectionStringValue" value="" />
</appSettings>
This is how we have worked in our project.
Try this, maybe!
<appSettings file="ConnectionStrings.config">
<add key="CrystalImageCleaner-AutoStart" value="true"/>
<add key="CrystalImageCleaner-Sleep" value="60000"/>
<add key="CrystalImageCleaner-Age" value="120000"/>
</appSettings>

Config file XDT transformation InsertIfMissing is skipping children

I am having trouble doing config transform, adding app settings on nuget package install where element appSetting may or may not exist.
What I want to happen:
appSetting element does not exist
Insert appSetting element and its children
appSetting element exist
Insert children if missing
I only get one or the other to work, but not both occasions.
web.config.install.xdt
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing" >
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Example 1
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
</configuration>
appSettings element not present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" />
<add key="Swagger.Contact.Email" value="some#email.address" />
</appSettings>
</configuration>
Example 2
web.config BEFORE
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
appSettings element present before transformation.
web.config AFTER
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" maxRequestLength="51200" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="Other.Key" value="With Some Value" />
</appSettings>
</configuration>
Nothing happens in example 2 as the appSettings element already exist, I would like it to still evaluate its child elements and insert those if they do not exist, but it seems they are just ignored. Is there any other value for the attribute xdt:Transform I can use, or any other hacks to work around this issue?
I had a similar problem quite a while back. The workaround I applied was to have two entries with <appSettings> in the XDT file, one to check if its absent and if yes, then go ahead and insert it. The other was for the scenario when the <appSettings> element was already present. Here is short snippet to help you with your problem:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings xdt:Transform="InsertIfMissing">
</appSettings>
<appSettings>
<add key="Swagger.Contact.Name" value="Some Name" xdt:Transform="InsertIfMissing" />
<add key="Swagger.Contact.Email" value="some#email.address" xdt:Transform="InsertIfMissing" />
</appSettings>
</configuration>
Let me know if this works for you.

IIS 10.0 Detailed Error: mimeMap

Hello so I'm pretty new to using IIS and Visual Studio but I've managed to launch one website on IIS but when I try to launch this one I get this error across the page:
Error message:
Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.mp4'
There's a section that reads Config Error:so here is my web.config file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="DetailLogging" value="True"/>
<add key="LoggingConfigFile" value="log4net.config"/>
<add key="DatabaseConnectionString" value="Integrated Security=True; MultipleActiveResultSets=True; Data source=.\SQLEXPRESS; Initial Catalog=WebEvaluation" />
<add key="Email" value="***********************"/>
<add key="SupportEmailID" value="*************************"/>
<add key="ForgotPasswordEmailSubject" value="Butterfly Scheme e-Certification Admin Console - Password Request"/>
<add key="WelcomeMailSubject" value="Butterfly Scheme e-Certification Admin Console"/>
<add key="PasswordChangedMailSubject" value="Butterfly Scheme e-Certification Admin Console - Password Changed"/>
<add key="PasscodeDeleteMailSubject" value="Passcode delete notification - "/>
<add key="Host" value="***********"/>
<add key="PortNo" value="25"/>
<add key="Password" value="***********"/>
<add key="SiteURL" value="***********************************"/>
<add key="RecordsPerPage" value="20" />
<add key="PasscodeCreateMailSubject" value="Butterfly Scheme e-Certification Passcode List created for : "/>
<add key ="XlsConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;IMEX=1'"/>
<add key ="XlsxConnectionString" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 8.0;'"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=******************"/>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=******************"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<httpRuntime requestValidationMode="2.0" maxRequestLength="2097151"
executionTimeout="900"/>
<sessionState mode="InProc" timeout="20"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<staticContent>
<mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
<mimeMap fileExtension=".m4v" mimeType="video/m4v"/>
<mimeMap fileExtension=".3gp" mimeType="video/3gpp"/>
</staticContent>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147482624"/>
</requestFiltering>
</security>
</system.webServer>
</configuration>
You could add something like this:
<remove fileExtension=".mp4" />
Or, since the MP4 mime type seems to be already present in the roots configuration file, simply omit the whole line:
<mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
If you really want to be sure that the extensions you seem to require are present, this could be a replacement for the staticContent tag:
<staticContent>
<remove fileExtension=".mp4" />
<remove fileExtension=".m4v" />
<remove fileExtension=".3gp" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
<mimeMap fileExtension=".m4v" mimeType="video/m4v"/>
<mimeMap fileExtension=".3gp" mimeType="video/3gpp"/>
</staticContent>
Could not load type "PresentationLayer.Global"
Check if you namespace is PresentationLayer or is changed? If changed update it in the markup, by right clicking on Global.asax and select "View Markup"

Can I create a virtual directory in web.config?

I have read that the applicationhost.config file sets the defaults and all directories below root inherit those settings.
I know its possible to change the defaultdirectory in a web.config, but I can't seem to do the same with a virtualdirectory.
Any pointers, help is appreciated.
Note: WEB.CONFIG file below DOES NOT work... just there to explain what I want to.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" existingResponse="Auto"/>
<asp scriptErrorSentToBrowser="true"/>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="Default.aspx" />
<add value="default.html" />
</files>
</defaultDocument>
<httpProtocol>
<customHeaders>
<clear />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.applicationHost>
<sites>
<site name="wed" id="1" serverAutoStart="true">
<application path="/alpha-beta">
<virtualDirectory path="/" physicalPath="\url.com\wwwroot\alpha\beta" />
</application>
</site>
<applicationDefaults applicationPool="DefaultAppPool" />
<virtualDirectoryDefaults allowSubDirConfig="true" />
</sites>
</system.applicationHost>
</configuration>

Moving <httpredirect> out of web.config in separate config file

We have many (more than 100) redirects in our web.config like
<configuration>
<system.webServer>
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
..... more than 100
</httpRedirect>
</system.webServer>
</configuration>
Is there way we can have this section managed in separate web.config or any other best solution?
You can move some config elements into their own config file to reduce clutter in the web.config.
<configuration>
<system.webServer>
<httpRedirect configSource="httpRedirects.config" />
</system.webServer>
</configuration>
This is achieved by adding the configSource attribute as shown above.
And in your seperate httpRedirects.config
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="/a" destination="/a/dfdf/default.htm" />
<add wildcard="/sad" destination="/aasd/dfdf/defsadault.htm" />
<add wildcard="/asdsaa" destination="/aasdas/dfasddf/default.htm" />
<add wildcard="/aasdsa" destination="/asdsaa/dfdf/defsdault.htm" />
<add wildcard="/aasd" destination="/adsa/dfdf/default.htm" />
</httpRedirect>
Note I have only tried this with other config elements.
You can store that in Separate Config file as shown here: SectionInformation.ConfigSource Property
In order to avoid cluttering the configuration file - web.config - it can be defined in a separate configuration file. That file can then be referenced from the web.config file as below:
<httpRedirect configSource="httpRedirects.config" />
The configSource attribute tells IIS configuration that the <httpRedirect> section is defined in a separate file httpRedirects.config.
EDIT:
Please make sure you have httpRedirect attribute set to enabled=true as the default value is false.
<httpRedirect enabled="true" configSource="httpRedirects.config" />

Resources