Share via


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

  1. Navigate to one of the language-specific subdirectories under the CAS directory, using the command prompt.

  2. 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

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories under the CAS directory.

  2. 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.

  3. 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

  1. In the Command Prompt window, navigate to the directory that contains the new executable file.

  2. Type Permissions.exe at the command line.

NoteNote

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

  • 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