Code Access Security Technology Sample
This sample demonstrates code access security, a mechanism that grants or denies access to resources within a method call. For example, code written by Microsoft may be allowed to write to the disk while code from another company may be forbidden from accessing the disk. This control is enforced even if the code from both companies is used within a single application. This sample repeatedly attempts to access a disk file and an environment variable. Before each attempt, the code access permissions are changed to see the effect. For more information about code access security, see the comments in the source code files.
For information about using the samples, see the following topics:
To build the sample using the command prompt
Navigate to one of the language-specific subdirectories under the CAS directory, using the command prompt.
Type msbuild PermissionsCS.sln or msbuild.exe PermissionsVB.sln, depending on your choice of programming language. For example, in the directory C:\Documents and Settings\Your User Name\My Documents\Samples\Technologies\CAS\VB, type msbuild.exe PermissionsVB.sln at the command line to build the Visual Basic version.
To build the sample using Visual Studio
Open Windows Explorer and navigate to one of the language-specific subdirectories under the CAS directory.
Double-click the icon for PermissionsCS.sln or PermssionsVB.sln, depending on your choice of programming language, to open the file in Visual Studio 2005.
On the Build menu, click Build Solution.
The sample application will be created in the default \bin or \bin\Debug subdirectory.
To run the sample
In the Command Prompt window, navigate to the directory that contains the new executable file.
Type Permissions.exe at the command line.
Note |
---|
This sample builds a console application. You must launch and run it in the Command Prompt window to view its output. |
Requirements
.NET Framework Version: 2.0, 1.1
Remarks
The technologies and classes used by this sample include the following:
Security
PermissionSet - Manages a set of permissions, as well as to Assert and Deny the permission in the stack.
CodeAccessPermission - Used for its static methods RevertDeny and RevertAssert, which revert the effects of Assert and Deny functions.
FileIOPermission - Manages permission to make file system requests such as read and write.
EnvironmentPermission - Manages permission to access environment variables.
I/O
FileStream - Opens, reads, and writes files in the sample.
Path - Gets the full path of a file. This can be particularly important when dealing with FileIOPermissions, because the system bases the security on the full path of a file.
Environment Variables
- Environment - Reads the environment variables for the process.
See Also
Reference
CodeAccessPermission
Environment
EnvironmentPermission
FileIOPermission
FileStream
Path
PermissionSet
System.IO
System.Security.Permissions
Other Resources
Code Access Security
File and Stream I/O
Key Security Concepts