# Microsoft's .NET Security Docs Now Say Stop Doing These 7 Things

URL: https://www.devopsness.com/blog/dotnet-security-what-microsoft-says-to-stop-doing
Author: Kiril Urbonas
Published: 2026-11-25
Updated: 2026-11-25
Categories: DevOps
Tags: Dotnet, Cryptography, AppSec, Secure Coding, Security

Summary: BinaryFormatter throws, CBC without a MAC is unsafe, CAS is not a boundary and the Rfc2898DeriveBytes constructors are obsolete. The seven habits Microsoft's own .NET docs now reject, and what to use instead.

---
# Microsoft's .NET Security Docs Now Say Stop Doing These 7 Things

Most insecure .NET code is not exotic. It is code that was correct in 2012 and still compiles. Microsoft's own security docs now reject seven of those habits outright, from BinaryFormatter to CBC decryption without a MAC. If your codebase predates .NET 6, assume at least two of them are in it.

> [!VERIFIED]
> Basis: docs
> .NET 9 and .NET 10, against the Microsoft Learn .NET security section (secure coding guidelines, cryptography model, cryptographic services, cross-platform cryptography, CBC timing vulnerabilities), the BinaryFormatter security guide, the SYSLIB obsoletions list and the OWASP Password Storage Cheat Sheet. The C# snippets and the build flag were compiled and run on .NET SDK 10.0.100. Checked November 2026.

The wording in these pages is unusually blunt for vendor docs. "BinaryFormatter is insecure and can't be made secure." "Do not use Code Access Security (CAS)." "Microsoft believes that it's no longer safe to decrypt data encrypted with the Cipher-Block-Chaining (CBC) mode..." Each line maps to a pattern that still ships in production.

## The seven, with the replacement for each

| Stop doing | Where Microsoft says so | Use instead |
|---|---|---|
| `BinaryFormatter` (and `SoapFormatter`, `LosFormatter`, `NetDataContractSerializer`) | Throws on use from .NET 9; SYSLIB0011 | `System.Text.Json`, `XmlSerializer`, `DataContractSerializer` |
| CAS or partial trust as a sandbox | SYSLIB0003; secure coding guidelines | OS users, AppContainers, containers, Hyper-V isolation |
| CBC decrypt without a verified MAC | CBC timing vulnerabilities page | Encrypt-then-MAC with constant-time compare, or `AesGcm` |
| MD5, SHA-1, ECB, TripleDES, DSA for new work | Cryptographic services, cryptography model | SHA-256/384/512, AES, RSA or ECDsa |
| `RNGCryptoServiceProvider`, `AesManaged`, `RijndaelManaged` | SYSLIB0023, SYSLIB0021, SYSLIB0022 | `RandomNumberGenerator` statics, `Aes.Create()` |
| `new Rfc2898DeriveBytes(...)` | SYSLIB0041 (.NET 7), SYSLIB0060 (.NET 10) | `Rfc2898DeriveBytes.Pbkdf2` with explicit SHA-256 and iterations |
| Assuming crypto behaves the same on every OS | Cross-platform cryptography | Test on each target OS; check the support matrix |

![Diagram: seven legacy .NET security patterns on the left, each with an arrow to the API Microsoft recommends instead on the right](/images/diagrams/dotnet-security-stop-start-map.svg)

## BinaryFormatter and CAS: the two that were never fixable

**BinaryFormatter** rebuilds whatever types the payload names, so a crafted stream runs code before your validation does. The guide says `Deserialize` "is never safe when used with untrusted input," that the denial-of-service cases "cannot be mitigated with a SerializationBinder," and that ".NET considers this behavior to be by design." Starting in .NET 9 the in-box implementation throws, and the switches that used to re-enable it are gone. The mechanism is the same one covered in [insecure deserialization explained](/blog/insecure-deserialization-explained): the format carries type instructions, not just data.

**Code Access Security** is the older trap. The secure coding guidelines say CAS and security-transparent code "are not supported as a security boundary with partially trusted code," and the same list adds "Do not use .NET Remoting" and "Do not use binary formatters." If a plugin system runs untrusted assemblies in-process and calls that a sandbox, it has no sandbox. The isolation has to come from the OS: a separate user, an AppContainer or a container.

## CBC without a MAC is a padding oracle

The CBC page is the one most teams have never read. Decrypting CBC data with verifiable padding, before checking integrity, lets an attacker learn plaintext by watching whether decryption fails on padding. Microsoft warns that an attacker "can detect very small (less than 0.1 ms) differences in execution time on remote systems," so hiding the error message does not help.

The guidance is direct: "Microsoft now recommends always doing encrypt-then-sign." Compute an HMAC over the IV and ciphertext, verify it with a constant-time comparison, and only then decrypt. The alternative is an authenticated mode where the tag check is built in:

```csharp
// Crypto/Seal.cs: AES-GCM, 96-bit nonce, 128-bit tag, never reuse a nonce per key
using System.Security.Cryptography;

static (byte[] nonce, byte[] cipher, byte[] tag) Seal(byte[] key, byte[] plain)
{
    var nonce = RandomNumberGenerator.GetBytes(12);
    var cipher = new byte[plain.Length];
    var tag = new byte[16];
    using var gcm = new AesGcm(key, tagSizeInBytes: 16);
    gcm.Encrypt(nonce, plain, cipher, tag);
    return (nonce, cipher, tag);
}
```

`AesGcm` runs on Windows, Linux, macOS and Android but not in the browser, accepts only 96-bit nonces, and Apple platforms limit the tag to 128 bits. Reusing a nonce under the same key breaks GCM, so generate it randomly or from a counter you can prove never repeats.

## Old primitives and the obsolete constructors

The cryptographic services page says "MD5 and SHA-1 algorithms have been found to be insecure per NIST SP 800-131A" and stay only for compatibility. It calls ECB "not considered secure" and recommends RSA over DSA. The cryptography model lists TripleDES among algorithms "no longer recommended."

The compiler already flags the rest. SYSLIB0023 says to replace `RNGCryptoServiceProvider` with the `RandomNumberGenerator` static methods. SYSLIB0021 retires derived types such as `AesManaged` in favor of `Aes.Create()`.

Password hashing changed twice. SYSLIB0041 (.NET 7) flagged constructors whose default hash and iteration count "are outdated and insecure": SHA-1 and 1,000 iterations, the values the doc tells you to pass only to read existing hashes. SYSLIB0060 (.NET 10) obsoletes every `Rfc2898DeriveBytes` constructor in favor of the one-shot `Rfc2898DeriveBytes.Pbkdf2`. OWASP's current figure for PBKDF2-HMAC-SHA256 is 600,000 iterations:

```csharp
// Auth/PasswordHash.cs
var salt = RandomNumberGenerator.GetBytes(16);
var hash = Rfc2898DeriveBytes.Pbkdf2(
    password, salt, iterations: 600_000, HashAlgorithmName.SHA256, outputLength: 32);
// verify with CryptographicOperations.FixedTimeEquals(hash, stored)
```

Store the iteration count next to the hash so you can raise it later and rehash on the next login.

## Crypto is the OS, not the runtime

The cross-platform page opens with the fact that surprises teams moving to Linux containers: "Cryptographic operations in .NET are done by operating system (OS) libraries." Your Alpine image's OpenSSL decides what works. As of .NET 10, OpenSSL support on macOS is gone (`RSAOpenSsl`, `ECDsaOpenSsl` and AES-CCM included), AES-CCM is unsupported on macOS and iOS, and SHA-3 is unsupported on Apple platforms. The post-quantum algorithms new in .NET 10 (ML-KEM, ML-DSA, SLH-DSA) need OpenSSL 3.5.0 or later on Linux. If you are mid-upgrade from the [.NET 8 end of support](/blog/dotnet-8-end-of-support-november-10-lambda-azure-functions), run the test suite on the base image you ship, not only on a Windows dev box.

## Find them before an attacker does

Most of the list is already a compiler warning. Promote those to errors and grep for what the analyzers miss:

```bash
# Fail the build on the obsolete security APIs
$ dotnet build -warnaserror:SYSLIB0003,SYSLIB0011,SYSLIB0021,SYSLIB0022,SYSLIB0023,SYSLIB0041,SYSLIB0060
# Catch the patterns that compile clean
$ grep -rnE 'CreateDecryptor|CipherMode\.(ECB|CBC)|MD5\.Create|SHA1\.Create|TripleDES|SoapFormatter|LosFormatter|NetDataContractSerializer' --include=*.cs .
```

The base library defaults block ciphers to CBC, so also review every `CreateDecryptor` call. A CBC hit is not automatically a bug. It is a bug when nothing verifies a MAC before decryption runs.

## Questions people ask

### Is BinaryFormatter completely removed in .NET 9?

Not deleted, disabled. From .NET 9 the in-box implementation throws exceptions on use, and the settings that used to re-enable it were removed, so a runtime upgrade turns a latent vulnerability into a crash. Plan to move payloads to `System.Text.Json` or `DataContractSerializer`, and convert any stored BinaryFormatter blobs once, in a trusted job, before the runtime upgrade.

### Is AES-CBC insecure in .NET?

CBC itself is not broken. Decrypting CBC data with verifiable padding before checking its integrity is what Microsoft calls unsafe, because padding errors and timing leak plaintext. Use encrypt-then-MAC with HMAC-SHA256 and `CryptographicOperations.FixedTimeEquals`, or switch new formats to `AesGcm`, which verifies the tag before returning plaintext.

## The decision, concretely

- **Deserializing anything from outside the process?** Use `System.Text.Json` with concrete types. Remove BinaryFormatter instead of hunting for a binder that makes it safe; Microsoft says none exists.
- **Encrypting new data?** Use `AesGcm` with a fresh 96-bit nonce. Keep CBC only for existing formats, wrapped in encrypt-then-MAC.
- **Hashing passwords?** Use `Rfc2898DeriveBytes.Pbkdf2` with SHA-256 and 600,000 iterations, and store the count.
- **Running untrusted plugins?** Put them in a separate process under a separate OS identity. CAS will not help.

## The call we'd make

Turn on the SYSLIB warnings as errors this sprint, because that one build flag finds most of this list for free. Then fix BinaryFormatter and CBC-without-MAC first, since those two are remotely exploitable, and leave the RNG and PBKDF2 swaps for the next upgrade. The caveat: changing the KDF or cipher breaks existing stored data, so ship a versioned format and migrate on read.

## Sources

- [Microsoft Learn: BinaryFormatter security guide](https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide)
- [Microsoft Learn: Timing vulnerabilities with CBC-mode symmetric decryption using padding](https://learn.microsoft.com/en-us/dotnet/standard/security/vulnerabilities-cbc-mode)
- [Microsoft Learn: Cross-platform cryptography in .NET](https://learn.microsoft.com/en-us/dotnet/standard/security/cross-platform-cryptography)
- [Microsoft Learn: SYSLIB obsoletions overview](https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/obsoletions-overview)

