现在的位置: 首页 > 综合 > 正文

[C#]解决多语言操作系统找不到everyone NTAccount问题

2013年10月21日 ⁄ 综合 ⁄ 共 991字 ⁄ 字号 评论关闭
            NTAccount acct = new NTAccount("Everyone");
            FileSystemAccessRule allowRule = new FileSystemAccessRule(acct, FileSystemRights.FullControl,
                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                PropagationFlags.None, AccessControlType.Allow);

这两行代码运行在英文系统下一切正常,但如果在多语言(如葡萄牙、瑞典、芬兰)系统下则会引发异常,异常信息如:
The conversion of some or all identifying references has failed或
Some or all identity references could not be translated.

 

也就是在多语言系统找不到对应的“Everyone” NTAccount,那问题自然就转移到如何才能在多语言系统找到匹配的“everyone”呢?这得感谢该帖子:http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/0737f978-a998-453d-9a6a-c348285d7ea3/
直接找到答案了,代码调整如下:

            SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            NTAccount acct = sid.Translate(typeof(NTAccount)) as NTAccount;

            FileSystemAccessRule allowRule = new FileSystemAccessRule(acct, FileSystemRights.FullControl,
                InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                PropagationFlags.None, AccessControlType.Allow);

WellKnownSidType.WorldSid的Summary是:Indicates a SID that matches everyone.

抱歉!评论已关闭.