Search code examples
asp.netvisual-studiovisual-studio-2015one-click-web-publishing

Replacing machinekey values using web.config transforms


In my web.config file I have the following entry:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <machineKey validationKey="656....9CCF" decryptionKey="9020.....659" validation="SHA1" decryption="AES" />
    </system.web>
</configuration>

I need to swap the validationKey and decryptionKey values under certain web publish profiles using the web config transform method. I'm struggling however, as I can't find any examples that achieve more than a basic connection string swap, or suchlike.

Is it possible to actually modify this part of the file using config transforms?

My attempt so far doesn't get recognised when I preview the transform...

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.web>
        <machineKey validationKey="AE3E7...FAB49" decryptionKey="ADS32....32423twe" xdt:Transform="Replace" xdt:Locator="Match(validationKey)" />
    </system.web>
</configuration>

Solution

  • You can use something like this:

    <machineKey validationKey="AE3E7...FAB49" decryptionKey="ADS32....32423twe" 
             xdt:Transform="SetAttributes" xdt:Locator="XPath(../machineKey)"/>
    

    Note that I replaced the xdt:Transform to "SetAttributes" not "Replace".

    For more reference you can check msdn page.

    You can also test the transform here.