アクセス権設定で"ALL APPLICATION PACKAGES"アカウントを指定すると例外発生
以下は「指定されたフォルダにアクセス権が付与されているアカウントを列挙し、それをそのまま再設定する」という単純なC#のプログラムですが、
using System; using System.IO; using System.Security.AccessControl; using System.Security.Principal; namespace SetAccessRuleExperimention { class Program { static void Main(string[] args) { string folderPath = "."; if (args.Length > 0) folderPath = args[0]; Console.WriteLine("folderPath={0}", Path.GetFullPath(folderPath)); DirectorySecurity accessControl = new DirectoryInfo(folderPath).GetAccessControl(); AuthorizationRuleCollection accessRules = accessControl.GetAccessRules(true, true, typeof(NTAccount)); foreach (FileSystemAccessRule fileSystemAccessRule in accessRules) { Console.WriteLine("{0}->{1}", fileSystemAccessRule.IdentityReference.Value, fileSystemAccessRule.IdentityReference.IsValidTargetType(typeof(NTAccount)) ? "Valid" : "Invalid"); } Console.WriteLine("-----------------------------------"); foreach (FileSystemAccessRule fileSystemAccessRule in accessRules) { Console.Write("{0}->", fileSystemAccessRule.IdentityReference.Value); accessControl.SetAccessRule(fileSystemAccessRule); Console.WriteLine("OK!"); } Console.ReadLine(); } } }
これをWin8以降で追加された"ALL APPLICATION PACKAGES"アカウントについて何らかのアクセス権が付与されたフォルダに対して実行すると、
APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES->
System.Security.Principal.IdentityNotMappedException はハンドルされませんでした。
HResult=-2146233087
Message=ID 参照の一部またはすべてを変換できませんでした。
Source=mscorlib
StackTrace:
場所 System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
場所 System.Security.Principal.NTAccount.Translate(Type targetType)
場所 System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
場所 System.Security.AccessControl.CommonObjectSecurity.SetAccessRule(AccessRule rule)
場所 System.Security.AccessControl.FileSystemSecurity.SetAccessRule(FileSystemAccessRule rule)
という例外が発生して死んでしまいます(Win8.1無印/with Bing/Professionalいずれのエディションでも発生)。
FileSystemAccessRule.IdentityReference.IsValidTargetType(typeof(NTAccount))での事前検証の結果は"True"(Valid)なのに何で?と思っていろいろ試行錯誤したところ、
- "APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES"という正式名(フルネーム)を与えると死ぬ
- "ALL APPLICATION PACKAGES"という通称名(省略された名前)だと大丈夫
という不可解な結果が得られました。つまり、
... foreach (FileSystemAccessRule item in accessRules) { FileSystemAccessRule fileSystemAccessRule = item; if (fileSystemAccessRule.IdentityReference.Value.Equals(@"APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES", StringComparison.CurrentCultureIgnoreCase)) { fileSystemAccessRule = new FileSystemAccessRule("ALL APPLICATION PACKAGES", fileSystemAccessRule.FileSystemRights, fileSystemAccessRule.InheritanceFlags, fileSystemAccessRule.PropagationFlags, fileSystemAccessRule.AccessControlType); } ...
というふうに"APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES"→"ALL APPLICATION PACKAGES"へ置換すれば、
ALL APPLICATION PACKAGES->OK!
になるのですが、これってOSのバグでは?