home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Permissions_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-05-01  |  7.0 KB  |  212 lines

  1. ' =====================================================================
  2. '  File:      Permissions.vb
  3. '
  4. '  Summary:   Demonstrates how use code access security.
  5. '
  6. ' ---------------------------------------------------------------------
  7. '  This file is part of the Microsoft .NET Framework SDK Code Samples.
  8. '
  9. '  Copyright (C) Microsoft Corporation.  All rights reserved.
  10. '
  11. ' This source code is intended only as a supplement to Microsoft
  12. ' Development Tools and/or on-line documentation.  See these other
  13. ' materials for detailed information regarding Microsoft code samples.
  14. '
  15. ' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. ' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. ' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. ' PARTICULAR PURPOSE.
  19. ' =====================================================================*/
  20.  
  21. Option Explicit On 
  22. Option Strict On
  23.  
  24.  
  25. '  Add the classes in the following namespaces to our namespace
  26. imports System
  27. imports System.IO
  28. imports System.Security.Permissions
  29. imports System.Security
  30.  
  31. public Module Permissions
  32.  
  33. ' /////////////////////////////////////////////////////////////////////////////
  34.  
  35.  
  36. '  This class represents the application itself
  37. class App
  38.     public sub Run()
  39.         '  Try to access resources using the permissions currently available.
  40.         AttemptAccess("Default permissions")
  41.  
  42.         '  Create a permission set that allows read access to the TEMP 
  43.         '  environment variable and read, write, and append access to SomeFile
  44.         dim ps as new PermissionSet(PermissionState.None)
  45.         dim fs as FileIOPermission
  46.         dim ep as EnvironmentPermission    
  47.  
  48.             fs = New FileIOPermission(FileIOPermissionAccess.Read Or FileIOPermissionAccess.Write Or FileIOPermissionAccess.Append, Path.GetFullPath("SomeFile"))
  49.         ep = new EnvironmentPermission(EnvironmentPermissionAccess.Read, "TEMP")
  50.  
  51.         ps.AddPermission(ep)
  52.         ps.AddPermission(fs)
  53.  
  54.         ' Use caution in asserting permissions in publicly callable code without
  55.         ' any kind of check on the caller.  There is a danger of the assert being
  56.         ' used to exploit a downstream caller by stopping its security check, 
  57.         ' allowing the malicious code access to unauthorized resources.
  58.  
  59.         ' Stop security checks at this point in the stack walk
  60.         ' for the specified permissions
  61.         ps.Assert()
  62.  
  63.         '  Try to access resources using the permissions we've just asserted.
  64.         AttemptAccess("Assert permissions")
  65.  
  66.         '  Remove this stack frame's Assert
  67.         CodeAccessPermission.RevertAssert()
  68.  
  69.  
  70.         '  Deny access to the resources we specify
  71.         ps.Deny()
  72.  
  73.         '  Try to access resources using the permissions we've just denied.
  74.         AttemptAccess("Deny permissions")
  75.  
  76.         '  Remove this stack frame's Deny so we're back to default permissions.
  77.         CodeAccessPermission.RevertDeny()
  78.  
  79.         '  Make the permissions indicate the only things that we're allowed to do.
  80.         ps.PermitOnly()
  81.  
  82.         '  Try to access resources using only the permissions we've just permitted.
  83.         AttemptAccess("PermitOnly permissions")
  84.  
  85.         '  Remove this stack frame's PermitOnly so we're back to default permissions.
  86.         CodeAccessPermission.RevertPermitOnly()
  87.  
  88.  
  89.  
  90.         '  Remove the FileIOPermissions from the permission set
  91.         ps.RemovePermission(fs.gettype())
  92.  
  93.  
  94.         '  Try to access resources using only the Environment permissions.
  95.         ps.PermitOnly()
  96.         AttemptAccess("PermitOnly without FileIOPermission permissions")
  97.         CodeAccessPermission.RevertPermitOnly()
  98.  
  99.         '  Remove the EnvironmentPermission from the permission set
  100.         ps.RemovePermission(ep.GetType())
  101.  
  102.  
  103.         '  Try to access resources using no permissions.
  104.         ps.PermitOnly()
  105.         AttemptAccess("PermitOnly without any permissions")
  106.         CodeAccessPermission.RevertPermitOnly()
  107.  
  108.         ' Show how to use Demand/Assert to improve performance
  109.         CopyFile(".\\Permissions.exe", ".\\Permissions.copy.exe")
  110.  
  111.         ' Delete .exe copy
  112.         File.Delete(".\\Permissions.copy.exe")
  113.  
  114.     end sub
  115.  
  116.  
  117.     public sub AttemptAccess(s as String)
  118.         dim fs as FileStream
  119.         dim ev as String = ""
  120.  
  121.         '  Try to access a file
  122.         try
  123.             fs = new FileStream("SomeFile", FileMode.OpenOrCreate) 
  124.         catch e as Exception
  125.         end try
  126.  
  127.  
  128.  
  129.         '  Try to read an environment variable
  130.         try
  131.             ev = Environment.GetEnvironmentVariable("TEMP")
  132.         catch e as Exception
  133.         end try
  134.  
  135.  
  136.  
  137.         '  Display what we sucessfully did.
  138.         dim str as String
  139.  
  140.         if Not fs Is nothing then
  141.             str = "FileOpen"
  142.         else
  143.             str = "FileNotOpened"
  144.         end if
  145.  
  146.  
  147.         str = str & ", "
  148.  
  149.         if ev <> "" then
  150.             str = str & "EVRead"
  151.         else
  152.             str = str & "EVNotRead"
  153.         end if
  154.  
  155.         Console.WriteLine(s & " test: " & str)
  156.  
  157.  
  158.         '  If we opened the file, close it & delete it
  159.         if Not fs Is nothing then 
  160.             fs.Close() 
  161.             File.Delete("SomeFile")
  162.         end if
  163.  
  164.     end sub
  165.  
  166.  
  167.     public shared sub CopyFile(srcPath as String, dstPath as String)
  168.  
  169.         '  Create a file permission set indicating all of this method's intentions.
  170.         dim fp as FileIOPermission = new FileIOPermission(FileIOPermissionAccess.Read, Path.GetFullPath(srcPath))
  171.             fp.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Append, Path.GetFullPath(dstPath))
  172.  
  173.         '  Verify that we can be granted all the permissions we'll need.
  174.         fp.Demand()
  175.  
  176.         '  Assert the desired permissions here.
  177.         fp.Assert()
  178.  
  179.         '  For the remainder of this method, demands for source file read access
  180.         '  and demands for destination file write/append access will be granted
  181.         '  immediately; walking the remainder of the stack will not be necessary.
  182.  
  183.         dim srcFile as FileInfo = new FileInfo(srcPath)
  184.         dim dstFile as FileInfo = new FileInfo(dstPath)
  185.         dim src as Stream = srcFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read)
  186.         dim dst as Stream = dstFile.Open(FileMode.Create, FileAccess.Write, FileShare.None)
  187.         if srcFile.Length > Int32.MaxValue then 
  188.             throw new Exception("CopyFile requires that the source file be less than 2GB.")
  189.         end if
  190.  
  191.         dim buffer(Convert.ToInt32(srcFile.Length)) as Byte
  192.         src.Read(buffer, 0, Convert.ToInt32(srcFile.Length))
  193.         dst.Write(buffer, 0, Convert.ToInt32(srcFile.Length))
  194.  
  195.         src.Close()
  196.         dst.Close()
  197.  
  198.         ' We do not need a RevertAssert here because we are going out of scope
  199.     end sub
  200.  
  201. end class   ' Application
  202.  
  203.     sub Main()
  204.         dim obj as App = new App
  205.  
  206.         obj.Run
  207.     end sub
  208.  
  209. end Module  ' Permissions
  210.  
  211. ' /////////////////////////////// End of File /////////////////////////////////
  212.