What is the purpose of the PermissionSet attribute

2020-06-30 06:04发布

问题:

On the MSDN FileSystemWatcher Class page, it includes an example with the following class attribute:

 [PermissionSet(SecurityAction.Demand, Name="FullTrust")]

What is the purpose of this? When should it be included or not included?

The FileSystemWatcher Class help page is here: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

回答1:

The FileSystemWatcher class has a link demand for unrestricted CAS permissions. This means that it will verify that its direct caller (i.e. your code, if you're consuming the class directly) has unrestricted permissions.

Unfortunately, use of a link demand opens up potential security holes since the permissions of indirect callers (i.e. code that calls your code) are not verified by a link demand. This means that an indirect caller with restricted permissions may be able to manipulate your highly trusted code into doing something nefarious on its behalf that it would otherwise not have had the permissions to accomplish.

One of the ways to prevent an attack of this sort is to apply your own full demand for the same permission to any code that that consumes a type or member with a link demand. This will ensure that any indirect callers will be subjected to the same permission demand, thereby ensuring that they cannot do anything via your code that they would not have been able to do on their own. The MSDN sample code for the FileSystemWatcher demonstrates the application of this sort of full demand.