This sheet is past its review date. Deadlines or advice may have changed since 5 Nov 2026.
C# and .NET: which APIs and defaults turn into vulnerabilities?
Move every app off .NET 8 and 9 to .NET 10.0.14 or later now, as both lose support on 10 November 202614, and before your next release remove BinaryFormatter and set a fallback authorization policy. Judge everything else by whether outside data can pick a type, reach SQL, a command line, a path, a URL or raw HTML, or skip an authorization check.
If you only do five things
Review a C# pull request
Where does outside data enter?
Find the controllers, minimal API handlers, Razor Pages, Blazor components, SignalR hubs and queue consumers it touches. Each should bind a request type, not an EF Core entity.
Who may call it, on which record?
Ask why for every
[AllowAnonymous],AllowAnonymous(),DisableAntiforgery()and[IgnoreAntiforgeryToken]. Neither[Authorize]nor the fallback policy checks the record14.Does input reach a type name, SQL, a process or a path?
Search for
TypeNameHandling,Type.GetType,FromSqlRaw,ExecuteSqlRaw,CommandText,Process.Start,Path.CombineandExtractToFile.Does it reach HTML or the network?
Check
Html.Raw,MarkupString,HttpClientcalls to URLs from input and everyServerCertificateCustomValidationCallback.Does it switch off the runtime's checks?
unsafeblocks,UnsafeandMemoryMarshalcalls,[DllImport]and[LibraryImport]need a second reviewer and a test.
C# and .NET traps and what to use instead
BinaryFormatterand its relatives.SoapFormatter,NetDataContractSerializer,LosFormatterandObjectStateFormatteralso let the payload pick the type9. .NET 9 and later throw onBinaryFormatter, but theSystem.Runtime.Serialization.Formatterspackage restores it, risks included. Read old payloads with the NRBF reader, which creates no types10.- Json.NET
TypeNameHandlingother thanNone. The JSON then names the type to create, and Newtonsoft says to vet it with aSerializationBinder11. KeepNone, never give a serializerType.GetType(name)with a name from data12, and in System.Text.Json declare subtypes with[JsonDerivedType]. - Known
machineKeyvalues in ASP.NET Framework. Microsoft found over 3,000 public keys usable for ViewState code injection13, and CISA lists Sitecore's default keys as exploited8. Generate unique keys, rotate them and encrypt themachineKeysection13. FromSqlRaw($"... {id}").FromSqlRawandExecuteSqlRawpaste interpolated values into the SQL;FromSqlandExecuteSqlsend each as aDbParameter16, and EF Core 10 warns on concatenation17. Map column names from a fixed list14.Process.Start("cmd.exe", "/c ..."). Start a fixed full path withUseShellExecute = false, not the .NET Framework default19, useArgumentListon .NET, and never pass input tocmd.exe,powershell.exeorsh -c18.Path.Combine(root, name). A rootednamediscardsroot, and Microsoft says to usePath.Joinfor user input20. That still lets..climb out, so requirePath.GetFullPath()of the result to start with the root plus a separator.HttpClient.GetAsync(url)with a URL from input. Server-side request forgery ranks 22nd in the CWE Top 257. Allowlist hosts, check the resolved address inSocketsHttpHandler.ConnectCallbackand setAllowAutoRedirect = false.Html.Raw,HtmlStringandMarkupString. They skip Razor's encoding1421, and cross-site scripting ranks 1st in the CWE Top 257. Sanitize user HTML with a maintained sanitizer.
Tools that catch it
- .NET analyzers. Set
<AnalysisModeSecurity>All</AnalysisModeSecurity>23, then fix CA2300 to CA2330 (deserializers), CA2100 and CA3001 (SQL), CA3006 (processes) and CA5359 (certificates) first24. - BannedApiAnalyzers flags every use of the APIs listed in
BannedSymbols.txt25. - NuGet Audit runs on every restore: add NU1903 and NU1904 to
<WarningsAsErrors>26. The supply-chain sheet covers the rest.
What changed in 2026
- .NET 8 and 9 reach end of support on 10 November1, and .NET Framework 4.6.2 on 12 January 20272.
- October's two .NET 10 releases fixed 46 CVEs, including critical Kestrel request smuggling (CVE-2026-88092)35. Two fixes were complete only in 10.0.144.
- Data Protection let attackers forge auth cookies in the NuGet package 10.0.0 to 10.0.6, mainly outside the shared framework on Linux and macOS (CVE-2026-40372). If you ran it exposed, rotate the key ring and tokens issued meanwhile6.
What you can skip
SecureString. Microsoft advises against it for new code, and it isn't encrypted outside Windows27.- A
SerializationBinderto tameBinaryFormatter. It may not stop code execution, and Microsoft doesn't commit to patching bypasses9. Migrate. - In-process sandboxes. Code Access Security and partial trust aren't supported security boundaries14. Use a separate process or container.
Mistakes that cost the most
- Patching the host but not the app. Self-contained apps stay vulnerable until recompiled and redeployed5, and October's Blazor WebAssembly fix needed updated package references4. Rebuild container images too.
- Unprotected Data Protection keys. Default storage encrypts them only on Windows, and naming your own store turns that off28. Add
ProtectKeysWithAzureKeyVault. - Development settings in production. Keep the
Developmentenvironment and unencrypted User Secrets on developer machines14.
What this looks like for your code
New project
- Start on .NET 10 with nullable checks, all security rules and warnings as errors.
- Set the fallback policy before the first endpoint.
- Reject unknown and duplicate JSON properties, as .NET 10's
JsonSerializerOptions.Strictdoes, but keep your naming policy22.
Existing codebase
- Leave .NET 8 and 9 now; move .NET Framework 4.6.2 to 4.8 or 4.8.1 before 12 January 20272.
- Set
<NuGetAuditMode>all</NuGetAuditMode>inDirectory.Build.props.