Is the web.config/app.config element configSections deprecated in DotNet4.5? - asp.net

I have an old application in ASP.Net (3.5) that needs upgrading, however, after updating to 4.5, a configSections element in the web.config file is not recognized and the app fails to load. Did something change here?
the following used to work...
<configuration>
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<!-- PayPal SDK settings -->
<paypal>
<settings>
<add name="mode" value="sandbox"/>
<add name="clientId" value="YOUR APPLICATION CLIENT ID"/>
<add name="clientSecret" value="YOUR APPLICATION CLIENT SECRET"/>
</settings>
</paypal>
</configuration>
but no joy
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Config Source:
22:
23: <configSections>
24: <section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />

I figured it out. It had nothing to do with permissions, IISExpress, or anything else, but only the position in the section. I moved element to just under the element and there's no error anymore. Go figure.
<configuration>
<configSections>
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
...
I've written my own XML parsers in the past that read the entire file and are not dependent on the order of the elements (just so long as it follows the structure & required elements are present). I incorrectly assumed that order does not matter in the web.config file. Good to know (probably save lots of coders headaches in the future).

Related

How to use ASP.NET 4.8 ConfigBuilders with system.serviceModel section of web.config

I have an ASP.NET 4.8 app that I am trying to integrate configuration builders into. I have installed the NuGet package Microsoft.Configuration.ConfigurationBuilders.Environment and added the required sections to Web.config (heavily truncated here).
<?xml version="1.0" encoding="utf-8"?>
<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="EnvironmentExpand" mode="Expand" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
</builders>
</configBuilders>
<system.serviceModel configBuilders="EnvironmentExpand">
....
</system.serviceModel>
</configuration>
When I run the application I get an HTTP 500 response from every action, with the message Unrecognized attribute 'configBuilders'. Intellisense in Visual Studio also highlights configBuilders and says The configBuilders attribute is not allowed.
If I remove the configBuilders attribute from system.serviceModel the application runs correctly although of course none of the placeholders in that section are expanded.
If I remove the configBuilders attribute from system.serviceModel and add it to another section, for instance connectionStrings, the application runs and the placeholders in the connectionStrings section are replaced.
Clearly there's something different about the system.serviceModel section but I don't understand what or how I can work around it so that I can replace placeholders in there.
If you look at your machine.config, you'll see that system.serviceModel is a sectionGroup and not a section. ConfigurationBuilders only apply at the section level. WCF and ASP.Net for better or worse have their large configuration divided into multiple sections.

User Secrets in .NET 4.7 connectionstrings format

I have been digging for hours and keep coming up with information about .NET Core, yet hardly anything about .NET 4.7 full framework. I figured out how to add User Secrets to the main project of my Web API solution. I get the basic secrets.xml file where I need to to either store my database username and password or my connection string. Every post I find talks about the changes you need to make to web.config. However nothing shows what to do with my connection string, how to format it, in the secrets.xml file. I could create a name/value pair but that does not seem to do anything, my app cannot connect to the database.
I have this in my Web.config:
<configBuilders>
<builders>
<add name="Secrets" userSecretsId="5c65f7eb-a7e1-46cc-bff4-a526678005f2" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=1.0.0.0, Culture=neutral" /></builders>
</configBuilders>
<connectionStrings configBuilders="Secrets">
<add name="ShopAPDbConnectionString" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
My secrets.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<root>
<secrets ver="1.0">
<secret name="ShopAPDbConnectionString" value="Server=SQLDEV01;Database=ShopAP; Integrated Security=True;" />
</secrets>
</root>
How do I properly format and get this to work?
Here is what I was able to get to work based on https://github.com/aspnet/MicrosoftConfigurationBuilders
Web.config
<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="Secrets" userSecretsFile="~/../../../SecretsTest/secrets.xml" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Json" jsonFile="${JSONConfigFileB}" optional="true" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</builders>
</configBuilders>
<!--...-->
<appSettings configBuilders="Secrets">
<!--...-->
</appSettings>
<!--...-->
<connectionStrings configBuilders="Json">
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="dummy.value.required" />
</connectionStrings>
<!--...-->
</configuration>
secrets.xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<secrets ver="1.0">
<secret name="usersecret1" value="dogodog" />
<secret name="usersecret2" value="secretbar" />
<secret name="JSONConfigFileB" value="C://Users//xxx//Documents//xxx//xxx//SecretsTest//settings.json" />
</secrets>
</root>
settings.json
{
"DefaultConnection": "Server=666.66.666.6;Database=BigToe;User ID=FireBall;Password=BunniesAreSoft",
}
Both your files look fine - pretty much exactly the same as mine.
How did you create your secrets file? Did you use right-click on the Web project and Manage User Secrets?
That will create a file in a folder location in %APPDATA% that should be picked up when you hit F5 in Visual Studio (Debug > Start Debugging). However, it will not be seen by IIS if you compile and then browse to localhost. Even if you change the App Pool Identity in inetmgr to run under your account, and set Load User Profile - it still will not find it.
To find the correct location for the secrets for this case, run your web app and obtain the result of
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
and copy the secrets file into the matching sub-folder Microsoft\UserSecrets\(guid)\
e.g.
C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft\UserSecrets\5c65f7eb-a7e1-46cc-bff4-a526678005f2\secrets.xml
you may need to one-off obtain access in Explorer, and create the UserSecrets folder and below.
Remember to update both copies if you edit it in future.
Make sure the only file in your UserSecrets folder (C:\Users...\UserSecrets{guid}) is the secrets.xml file. I was messing around and had a secrets.json file in there (along with the secrets.xml file) and it wouldn't load my secrets.xml file. After I removed the json file, it worked fine.
I am attempting to do the same thing, but unfortunately I think this is only for appSettings when it comes to .Net Framework/Web.config.

Avoid inherited ELMAH errors in an ASP.NET subapplication

I have a parent IIS application that uses ELMAH and a child ASP.NET application (virtual directory) that doesn't use ELMAH. When I try to browse my subapplication I get this error:
Could not load file or assembly 'Elmah' or one of its dependencies. The system cannot find the file specified.
Which is understandable as my child application's bin folder doesn't contain any ELMAH assemblies.
The problem probably is that the parent web.config file contains this:
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>
</configSections>
As far as I understand there is no way to stop <configSections> inheritance, see e.g. How to stop inheritance of <configSections>in Web.Config. Is there a way to run my ELMAH-free subapplication then?
You cannot keep your subapplication ELMAH-free because of the parent configuration. However - since I imagine you don't want to reference the assemblies from the parent - what you can do is tell your subapplication to look for assemblies in the parent bin folder with the probing configuration.
This way your subapplication doesn't need to know what assemblies exist for the parent, only that if an unknown assembly is needed it can be found in the parent folders
Edit: That's indeed a bummer. Clearing and removing configsections tags was considered too complex by Microsoft :
<clear /> and <remove /> were never implemented for configSections and sectionGroups because of the difficulty involved attempting to merge different definitions of the same section-handlers and section groups.
Hence the path workaround. You could also set up the applications so they are not related in terms of hierarchy, if this is critical
I have found that adding enableConfigurationOverride="false" to the application pool definition fixes this problem.
From the MSDN docs:
Optional Boolean attribute. When true, indicates that delegated settings in Web.config files will processed for applications within this application pool. When false, all settings in Web.config files will be ignored for this application pool.The default value is true.
There are two methods of doing this, where ChildApplicationName should be replaced with the value of your application pool name.
Method 1 (preferred/advised):
Execute the following command in appcmd in Administrator mode:
appcmd.exe set config -section:system.applicationHost/applicationPools /[name='ChildApplicationName'].enableConfigurationOverride:"False" /commit:apphost
Method 2:
The second involves directly editing the %windir%\System32\inetsrv\config\applicationHost.config file allows the application to effectively ignore the parent application. For various reasons, I don't advice doing this, but I leave this here for posterity.
In order to set this, do a search for your child application pool name. It should be under the xpath. /configuration/system.applicationHost/applicationPools.
<configuration>
<system.applicationHost>
<applicationPools>
...
<add name="ChildApplicationName" enableConfigurationOverride="false" />
...
<applicationPools>
<system.applicationHost>
<configuration>

web.config transform for microsoft.identitymodel configsection

How does one go about transforming the audienceUris and federatedAuthentication elements in the microsoft.identitymodel configuration section using the standard visual studio web.config transforms?
For the development environment, we have all the subdomains postfixed with a '-dev' along with special localhost port numbers, for example: realm="https://client-dev.domain.com:444". Today the main web.config reflects this but ideally we would like to move this setting to Web.Debug.config and in the Web.Release.config we would like to specify the proper audience and realms sans the '-dev' postfix and the local port settings. However, attempting to put xdt:Transform="Replace" in the microsoft.identitymodel element inside the Web.Debug.Config results in this warning:
The 'http://schemas.microsoft.com/XML-Document-Transform:transform' attribute is not declared.
and of course runtime failures (not being able to find servicecertificate for example).
Here's the microsoft.identitymodel sections:
<configuration>
<configSections>
<section name="microsoft.identityModel" type="Microsoft.IdentityModel.Configuration.MicrosoftIdentityModelSection, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<microsoft.identityModel>
<service>
<applicationService>
<claimTypeRequired>
<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier" optional="false" />
<claimType type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" optional="false" />
</claimTypeRequired>
</applicationService>
<serviceCertificate>
<certificateReference x509FindType="FindByThumbprint" findValue="NNNNNNNNNN" storeLocation="LocalMachine" storeName="My" />
</serviceCertificate>
<certificateValidation certificateValidationMode="None" />
<audienceUris>
<add value="https://url/" />
</audienceUris>
<federatedAuthentication>
<wsFederation passiveRedirectEnabled="true" issuer="https://url/" realm="https://url/" requireHttps="true" />
<cookieHandler requireSsl="true" />
</federatedAuthentication>
<issuerNameRegistry type="Microsoft.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<trustedIssuers>
<add thumbprint="NNNNNNNNNN" name="https://url/" />
</trustedIssuers>
</issuerNameRegistry>
</service>
</microsoft.identityModel>
<configuration>
Existing solutions that don't work:
1) Same issue was raised here but no proper response. The RemoveAll and Insert doesn't work:
Web config transform on microsoft.identityModel - 'http://schemas.microsoft.com/XML-Document-Transform' attribute is not declared
2) Also, tried to manipulate the namespace as suggested on this thread and this doesn't work either:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9ededb66-3e34-4bba-8e20-a2cc6025f0f9
3) configSource looked promising but again does work when specified in the microsoft.identitymodel element.
I get:
Warning 1 The 'configSource' attribute is not declared.
http://blog.andreloker.de/post/2008/06/16/Keep-your-config-clean-with-external-config-files.aspx
Any help will be appreciated! Thanks.
After a lot of trial and error, I resorted to using XMLPreProcess by Loren Halvorson to auto-generate the correct Web.config from a TemplateWeb.config as part of the pre-Build event for each project.
Although I would have liked Visual Studio's attempt at this to work for developers, alas, time is of money and its time for me to move on. Needless to say, it's broken in the SQL Azure SDK as well because the transforms only occured when you publish a site. In the latest Azure SDK, that is no longer the case and hence transforms stopped working as well. Oh joy!
Here's a link to the excellent tool, I think, a must-have for every ASP.NET developer:
http://xmlpreprocess.codeplex.com/

Why might this ASP.NET profile section fail to dynamically compile?

When the following profile section is in my web.config, the first page request fail with FileNotFoundException and a bunch of error coming from the compiler (from the look of the stack trace). When the properties section is removed, everything works again (until I try to reference something from the Profile of course). The rest of the website is pre-compiled, assembly per page, not-updatable.
The problem has only been reproduced on one machine-- the same build worked on other highly similar machines.
Am I missing anything obvious?
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider"
connectionStringName="ConnectionString"
applicationName="OIWebApps"
type="System.Web.Profile.SqlProfileProvider"/>
</providers>
<properties>
<group name="UI">
<add name="MasterPage" defaultValue="Horizontal.master"/>
<add name="Theme" defaultValue="Default"/>
<add name="IsSessionWarningEnabled" defaultValue="True" type="System.Boolean"/>
<add name="UseTelerikMultilineTextBoxes" defaultValue="True" type="System.Boolean"/>
<add name="FontSize" defaultValue="Smaller" type="System.String"/>
</group>
</properties>
</profile>
I see "UseTelereikMultilineTextBoxes". Could it be that a file needed for Telerik is missing on this machine?
It failed to compile because of a missing native .dll It was extremely hard to identify because the page that was executing didn't directly use the .net dll that depended on the missing native .dll, and even more confusingly, only some pages raised the compile error instead of being raised on all pages.

Resources