Common Language Runtime (CLR) Security

.NET Documentation

Introduction to CLR Security

The .NET Common Language Runtime (CLR) provides a robust and flexible security model to protect your applications and the underlying system from malicious or untrusted code. This model is based on a combination of code-based security, role-based security, and evidence-based policies.

Key Concepts

Security Features in Modern .NET

While the explicit CAS model has been de-emphasized in favor of operating system-level security and application-level authorization, the principles of least privilege and defense-in-depth remain paramount. Modern .NET development relies heavily on:

Example: Basic Permission Check (Conceptual)

In older .NET frameworks, you might have seen code like this to request a specific permission:


using System.Security.Permissions;

// ...

[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
public void SensitiveOperation()
{
    // Code that requires administrator privileges
    Console.WriteLine("Executing sensitive operation.");
}
            

In modern .NET, you would typically handle authorization at a higher level, perhaps within an ASP.NET Core controller or service, checking the authenticated user's roles or claims.

Note: For .NET Core and .NET 5+, the focus has shifted away from fine-grained CAS. Security is primarily handled through application-level authorization, OS-level controls, and secure development practices. Always refer to the latest .NET documentation for the most current security recommendations.

Resources