Web.Config Transformation Not Working - asp.net

Using VS 2k15, ASP.NET 4.5
My Transform is not working. I was looking in preview and the files are the same. Then I figured maybe I had to deploy to see transformation. So I set up a custom Deploy to my desktop, using the Deploy Configuration. Checked the web.config after publishing that, still matching the original.
Any idea what I'm doing wrong?
I know when I do the preview I get a warning at the top of the preview that says..."
These files have different encodings. Left file: Unicode (UTF-8) without signature. Right file: Unicode (UTF-8) with signature. You can resolve the difference by saving the right file with the encoding Unicode (UTF-8) without signature.
However, when I choose Save Options and save the Deploy file with no signature (so they're matching) the message still comes up. Just not sure what's going on. Any ideas why I can't this transform to work?
Web.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="fileLibrary" value="c:\vsoProjects\localFiles\rlFileLibrary" />
</appSettings>
<connectionStrings>
<add name="appConnString" connectionString="Data Source=(LocalDb);Initial Catalog=DevDB;User ID=*****;Password=*****;Connect Timeout=300" />
</connectionStrings>
</configuration>
Web.ContDeploy.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="fileLibrary" value="R:\rlFileLibrary" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
</appSettings>
<connectionStrings>
<add name="appConnString" connectionString="Data Source=myserver.test.com;Initial Catalog=DeployDB;User ID=*****;Password=*****;Connect Timeout=300" xdt:Transform="SetAttributes(connectionString)" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>

I'm not an expert on the transform, but my (working) transform for the appSettings key has this signature:
<appSettings>
<add key="datafolder" value="D:\sites\removedpath\App_Data\" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
which is slightly different than yours

I found the problem. This project was converted a while back from asp.net 2.x to 4.5.
The web.config still had an attribute up in the configuration section.
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
...
</configuration>
You can see it up top in the code I posted. I didn't realize it was still there. After all that time banging my head against my desk I just removed that attribute, preview transform, and voila it was there! Also deploys properly as well.

Related

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.

How to keep private data separate in an open source ASP.NET MVC application

In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.

Can't figure out web.debug.config vs. web.config substituation in VS2010

Can someone point me to what am I doing wrong here?
I'm trying to set up an ASP.NET web app project to compile with two versions of web.config file for Release and Debug builds. So for simplicity sake, here's my web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
And then I do the following in web.debug.config:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="WhyMicrosoftSucksSoMuch"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename='C:\FilePath\Database1.mdf';User Instance=true"
providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(connectionString)" />
</connectionStrings>
</configuration>
If I publish it under Debug configuration the resulting web.config looks good, but when I try to run my project from VS2010 also under Debug configuration I get an error when my logic attempts to access database:
The ConnectionString property has not been initialized.
So what's the trick here?
PS. And please don't point me to this document. I tried reading it several times but I get a headache from so much superfluous information. I guess MS doesn't know what brief is.
When running it under debug mode it doesn't apply any transformations.
It only applies them during publishing. You can put your debug connection string in the main web.config, and add your production connection string to the web.release.config
also, you probably will want to use
xdt:Transform="Replace"

Web.config Transform on another configuration file

Can you do a web.config transforms on another file like connections.config, nlog.config, app.config in the same way? I can't seem to find an answer.
<connectionStrings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="ApplicationServices" connectionString="Data Source=ConnectionString" xdt:Transform="SetAttributes(mode)"/>
</connectionStrings>
I have created a Visual Studio add in for just this. It's called SlowCheetah and you can find it at http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5.

Publish is not transforming web.config?

I made a web.config (full file, it doesn't show XML errors)
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
...
<location path="." inheritInChildApplications="false">
<connectionStrings>
<add name="ElmahLog" connectionString="data source=~/App_Data/Error.db" />
<add name="database" connectionString="w" providerName="System.Data.EntityClient"/>
</connectionStrings>
</location>
...
with a transform file (web.Staging.config)
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="database"
connectionString="c"
providerName="System.Data.EntityClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<customErrors defaultRedirect="error.aspx"
mode="RemoteOnly" xdt:Transform="Replace">
</customErrors>
</system.web>
</configuration>
I am publishing in Staging mode (right click website > Publish > Method: File System ...)
------ Build started: Project: Drawing, Configuration: Staging Any CPU ------
Drawing -> D:\Project\bin\Staging\Drawing.dll
------ Build started: Project: MySystem, Configuration: Staging Any CPU ------
MySystem -> D:\Project\bin\Staging\MySystem.dll
...
But when I look at the web.config in the output folder it isn't changed.
I found the following on the Build log:
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
Transformed web.config using Web.Staging.config into obj\Staging\TransformWebConfig\transformed\web.config.
What could be the problem? Am I doing this right?
Answering late but perhaps I can save someone a headache. In Visual Studio 2013, there are two places to select configuration for your build and deploy. The Configuration Manager and then again with Publish Web where the third step in the Wizard entitled Settings allows you to select Config you want to use. If you don't select your new configuration it will use the transform for the selected configuration instead of yours.
I found out two things:
You cannot set a namespace on the <configuration> tag (ex: for <location path="." inheritInChildApplications="false">)
You have to watch for the correct hierarchy in the transform file.
Like
<configuration>
<location>
<connectionStrings>
Instead of
<configuration>
<connectionStrings>
Ensure that in the properties of the Web.Config file Build Action is set to Content.
If the build action is set to None, it will not be transformed, even if it is being copied to the output directory.
Make sure to include InsertIfMissing if the section you are trying to add does not already appear in the output.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<location>
<system.webServer>
<security xdt:Transform="InsertIfMissing">
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</location>
</configuration>
Don't forget to copy all the other attributes of "configuration" from the original "web.config", as it seems that VS2012 doesn't do it automatically and of course there will be no match...
Answering late as well, but this may help someone.
I realized that if you have two websites in the same solution, when you try to publish one of them the transformation might not work if you have one only configuration for both projects.
One of my websites was always transforming, but the other sometimes was and sometimes wasn't.
For example, I had the configuration "Auto" in the solution, and had web.Auto.config for both websites.
I resolved that by creating a new configuration with a different name - "AutoAdmin" - creating also its web.AutoAdmin.config file for the second project, and when I published it again the transformation finally occurred.
I followed the below steps to fix this issue. Thanks, #michaelhawkins for pointing in the right direction. You need to make sure you change the configuration to release in two places.
And right click on your project and select "Properties". IF not working try selecting x86 in CPU Architecture
#Karthikeyan VK your post resolved my issue. Although I was selecting Production configuration in my publish profile, in configuration manager it was set to dev therefore It didn't transform my settings.
Microsoft needs to fix this bug. Once you pick a configuration in the publishing profile it should automatically update the configuration manager as well.

Resources