This documentation is archived and is not being maintained.

Writing Secure Class Libraries

Programming errors in class libraries can expose security vulnerabilities because class libraries often access protected resources and unmanaged code. If you design class libraries, you need to understand code access security and be careful to secure your class library.

The following table describes the three main elements you need to consider when securing a class library.

Security element


Security demand

Demands are applied at the class and method level as a mechanism for requiring that callers of your code have the permissions that you want them to have. Demands invoke a stack walk, in which all callers that directly or indirectly call your code are checked on the stack when your code is called. Demands are usually used in class libraries to help protect resources.

Security override

Overrides are applied on the class and method scope as a way to overrule certain security decisions made by the runtime. They are invoked when callers use your code. They are used to stop stack walks and limit the access of callers who have already been granted certain permissions.

The security changes in the .NET Framework version 4 have made some overrides obsolete. For information about these changes, see Security Changes in the .NET Framework 4.
Caution noteCaution
Overrides can be dangerous and should be used with care.

Security optimization

Transparency, which was introduced in the .NET Framework 4, provides new ways to optimize code access security. For more information, see Security-Transparent Code.