
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
980
|
Chapter 17: Security
Security optimization techniques, such as using Assert in this case (even though it
isn’t the primary reason to use
Assert), can help class library as well as control devel-
opers who are trusted to perform
Asserts in order to speed the interaction of their
code with the CLR; but if used improperly, these techniques can also open up holes
in the security picture. This example shows that you can have both performance and
security where secure access is concerned.
If you are using
Assert, be mindful that stackwalk overrides should never be made in
a class constructor. Constructors are not guaranteed to have any particular security
context, nor are they guaranteed to execute at a specific point in time. This lack leads
to the call stack not being well defined, and
Assert used here can produce unex-
pected results.
One other thing to remember with
Assert is that you can have only one active Assert
in a function at a given time. If you Assert the same permission twice, a
SecurityException is thrown by the CLR. You must revert the original Assert first
using
RevertAssert. Then you can declare the second Assert.
See Also
See the “CodeAccessSecurity.Assert Method,” “CodeAccessSecurity.Demand Method,”
“CodeAccessSecurity.RevertAssert Method,” and “Overriding Security Checks” ...