C# and .NET • Modern C# baseline
Secure C#: Validation, Secrets, and Safe APIs
Protect application boundaries by validating inputs and using secure framework APIs.
C# and .NET provide safer abstractions, but applications remain responsible for authorization, secret handling, encoding, and data validation. Use parameterized database APIs, context-aware output encoding, secure random generators for security tokens, and least-privilege service identities. Avoid logging credentials or trusting client-side validation.
Key points
- Validate authorization on the server for every protected operation.
- Use framework encoders instead of hand-written escaping.
Example
using System.Security.Cryptography;
byte[] token = RandomNumberGenerator.GetBytes(32);
string encoded = Convert.ToBase64String(token);
Console.WriteLine(encoded.Length);
What to expect
Creates 32 bytes from a cryptographically secure random-number generator and encodes them as text.
Practice exercise
Document how a generated token is stored, transmitted, expired, and protected from logs.
Key idea: Security is an end-to-end property of trust boundaries, permissions, storage, and output.