King Wilder
King Wilder

Reputation: 659

Nuget web.config.install.xdt not transforming

I'm having problems figuring out how to transform the web.config file when my NuGet package it installed. It's doing some of the transformations, but not all of them.

Here's the untouched web.config file that I need to modify upon installation of my NuGet package:

<?xml version="1.0" encoding="utf-8"?>
<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" />
  </appSettings>
  <system.web>
    <authentication mode="None" />  ***** I want this removed *****
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />  ***** I want this removed *****
    </modules>
  </system.webServer>
</configuration>

Here's what I want as the result:

<?xml version="1.0" encoding="utf-8"?>
<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="MvcMailer.BaseURL" value="" />
    <add key="SecurityGuardEmailFrom" value="[email protected]" />
    <add key="SecurityGuardEmailSubject" value="Your Password has been reset." />
    <add key="SecurityGuardEmailTemplatePath" value="~/MailerTemplates/ResetPassword.html" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/SGAccount/Login" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules>
    </modules>
  </system.webServer>
</configuration>

This is transformed web.config file in the MVC application, which is incorrect:

<?xml version="1.0" encoding="utf-8"?>
<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="MvcMailer.BaseURL" value="" />
    <add key="SecurityGuardEmailFrom" value="[email protected]" />
    <add key="SecurityGuardEmailSubject" value="Your Password has been reset." />
    <add key="SecurityGuardEmailTemplatePath" value="~/MailerTemplates/ResetPassword.html" />
  </appSettings>
  <system.web>
    <authentication mode="None" />  ***** Not removed when it should be *****
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/SGAccount/Login" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />  ***** Not removed when it should be *****
    </modules>
  </system.webServer>
</configuration>

And this is my web.config.install.xdt file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <authentication mode="None" xdt:Transform="Remove" xdt:Locator="Match(mode)" />
    <authentication mode="Forms" xdt:Transform="Insert">
      <forms loginUrl="~/SGAccount/Login" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" xdt:Transform="Remove" xdt:Locator="Match(name)" />
    </modules>
  </system.webServer>
</configuration>

I've read all the documentation on the Nuget.org site about how to use the XDT transformations, and it even works on this tester site; https://webconfigtransformationtester.apphb.com/, but it doesn't work in action.

I'm stumped. Any suggestions on how to make this work?

Upvotes: 1

Views: 2034

Answers (1)

King Wilder
King Wilder

Reputation: 659

Here's what the new web.config.install.xdt looks like that handled the job successfully:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="SecurityGuardEmailFrom" value="[email protected]" xdt:Transform="Insert" />
    <add key="SecurityGuardEmailSubject" value="Your Password has been reset." xdt:Transform="Insert" />
    <add key="SecurityGuardEmailTemplatePath" value="~/MailerTemplates/ResetPassword.html" xdt:Transform="Insert" />  
  </appSettings>
  <system.web>
    <authentication mode="Forms" xdt:Transform="SetAttributes" />
    <authentication mode="Forms">
      <forms loginUrl="~/SGAccount/Login" timeout="2880" xdt:Transform="Insert" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" xdt:Transform="Remove" />
    </modules>
  </system.webServer>
</configuration>

Instead of trying to Remove the original authentication element, I changed the mode attribute, then I Inserted the forms element. The rest seemed to work itself out once this worked correctly.

Upvotes: 1

Related Questions