You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EffectiveAccess.cs 1.8 KiB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Alphaleonis.Win32.Filesystem;
  2. using System;
  3. using System.Security.AccessControl;
  4. using System.Security.Principal;
  5. namespace Security2
  6. {
  7. public class EffectiveAccess
  8. {
  9. public static EffectiveAccessInfo GetEffectiveAccess(FileSystemInfo item, IdentityReference2 id, string serverName)
  10. {
  11. bool remoteServerAvailable = false;
  12. Exception authzAccessCheckException = null;
  13. var win32 = new Win32();
  14. var fss = new FileSystemSecurity2(item);
  15. var effectiveAccessMask = win32.GetEffectiveAccess(fss.SecurityDescriptor, id, serverName, out remoteServerAvailable, out authzAccessCheckException);
  16. var ace = new FileSystemAccessRule((SecurityIdentifier)id, (FileSystemRights)effectiveAccessMask, AccessControlType.Allow);
  17. return new EffectiveAccessInfo(
  18. new FileSystemAccessRule2(ace, item),
  19. remoteServerAvailable,
  20. authzAccessCheckException);
  21. }
  22. }
  23. public class EffectiveAccessInfo
  24. {
  25. private FileSystemAccessRule2 ace;
  26. private bool fromRemote;
  27. private Exception authzException;
  28. public FileSystemAccessRule2 Ace
  29. {
  30. get { return ace; }
  31. }
  32. public bool FromRemote
  33. {
  34. get { return fromRemote; }
  35. }
  36. public Exception AuthzException
  37. {
  38. get { return authzException; }
  39. }
  40. public bool OperationFailed
  41. {
  42. get
  43. {
  44. return authzException == null ? false : true;
  45. }
  46. }
  47. public EffectiveAccessInfo(FileSystemAccessRule2 ace, bool fromRemote, Exception authzException = null)
  48. {
  49. this.ace = ace;
  50. this.fromRemote = fromRemote;
  51. this.authzException = authzException;
  52. }
  53. }
  54. }