A pipeline step protects what your CI server builds. An MSBuild target protects what
every Release build produces — on the build server, on a developer's machine, in
dotnet publish and dotnet pack — because the step lives in the
project file itself. There is no package to install: it is a dozen lines of XML that call
the same API as the CI guide.
The target
Add this inside the <Project> element of the .csproj you ship:
<Target Name="ObfuscateAssembly"
AfterTargets="CoreCompile"
Condition="'$(Configuration)' == 'Release' And '$(FO_API_KEY)' != ''"
Inputs="@(IntermediateAssembly)"
Outputs="$(IntermediateOutputPath)obfuscated.stamp">
<PropertyGroup>
<_FoInput>@(IntermediateAssembly->'%(FullPath)')</_FoInput>
<_FoOutput>$(_FoInput).protected</_FoOutput>
</PropertyGroup>
<Exec Command="curl -sS --fail-with-body -X POST https://freeobfuscator.com/api/v1/obfuscate -H "X-Api-Key: $(FO_API_KEY)" -F "file=@$(_FoInput)" -F "encryptStrings=true" -F "controlFlow=true" -o "$(_FoOutput)"" />
<Copy SourceFiles="$(_FoOutput)" DestinationFiles="$(_FoInput)" />
<Delete Files="$(_FoOutput)" />
<Touch Files="$(IntermediateOutputPath)obfuscated.stamp" AlwaysCreate="true" />
</Target>
Then set the key as an environment variable named FO_API_KEY — MSBuild
exposes environment variables as properties — and build in Release:
dotnet build -c Release
Why it is written this way
-
It runs right after the compiler, on the intermediate assembly.
AfterTargets="CoreCompile"protects the file inobj/before MSBuild copies it anywhere. That matters:dotnet publishand single-file bundling take the assembly fromobj/, not frombin/. A target hooked afterBuildprotects thebin/copy and then publishes the unprotected one. -
It runs once per compile, not once per build. The
Inputs/Outputspair and the stamp file make the target incremental. Without them, a build where nothing changed would send the already protected assembly to be obfuscated a second time. -
Debug builds are untouched. The
Configurationcondition keeps the edit-compile-debug loop fast and debuggable. -
No key, no step. A teammate or contributor without
FO_API_KEYstill gets a working (unprotected) Release build rather than a failure. If you would rather a release can never ship unprotected, drop that half of the condition on your build server so a missing key fails the build. -
A failed call fails the build.
--fail-with-bodymakes curl return an error on an HTTP failure instead of saving the error message as your DLL, andExecstops the build on a non-zero exit code.
curl ships with Windows 10 and later, macOS and practically every Linux build
image, so the same target works on a developer PC and on any CI runner.
Choosing the layers
The example turns on string encryption and control-flow obfuscation. Add or remove
-F fields to match what you need — hideReferences=true,
virtualize=true with integrityCheck=true,
renamePublic=true for an application. A call with none of them does the
free-tier rename only. The
layer decision guide covers which
are worth it for your project.
Several projects
To protect more than one project, put the target in a
Directory.Build.targets file at the root of the repository — MSBuild imports
it into every project automatically. Make it opt-in so test and tool projects are not
sent for obfuscation: add And '$(ObfuscateOnBuild)' == 'true' to the
condition, and set
<ObfuscateOnBuild>true</ObfuscateOnBuild> only in the projects you
ship.
One case needs a different approach: assemblies that reach into each other's internals
through InternalsVisibleTo must be protected together, which a
per-project target cannot do. Build them unprotected and send the set to
/api/v1/obfuscate/set from a pipeline step
instead.
Things to know before you rely on it
- Release builds now need the network. That is the trade for having nothing installed. An offline build machine should use a pipeline step on a connected runner instead.
- Your tests run against the protected assembly if they run in Release. That is good for a smoke test and awkward for unit tests that reach internals — run the main suite in Debug, or see how to test an obfuscated build.
- Multi-targeted projects call the API once per target framework, which is what you want: every assembly you ship is protected.
- Sign afterwards. Signing steps must come after this target — obfuscation and code signing explains the order.
- Keep the rename map for releases. Add
-F "includeMap=true"in the build that produces a shipped version and archive the map, so you can decode crash reports later. The response is then a zip, so that variant belongs in a release pipeline step rather than this target.
Target or pipeline step?
Use the MSBuild target when you want protection to be a property of the
project — nobody can produce a Release build without it, and
dotnet publish and dotnet pack just work. Use a
pipeline step when you
want local builds to stay offline, need the rename map archived per release, or protect
several assemblies as a set. Plenty of teams use both: the target for day-to-day builds,
a pipeline step for the release artifact.