【发布时间】:2012-09-03 11:37:41
【问题描述】:
我试图在 UNC 路径上为特定用户授予 NTFS 权限,但我看到不同的行为取决于 UNC 路径。下面是我用来授予权限的代码(来自MSDN)以及每种情况下的结果,
static void GiveNTFSPermissions(string folderPath,
string ntAccountName,
FileSystemRights accessRights)
{
DirectorySecurity dirSecurity = Directory.GetAccessControl(folderPath);
FileSystemAccessRule newAccessRule =
new FileSystemAccessRule(
ntAccountName,
accessRights,
AccessControlType.Allow);
dirSecurity.AddAccessRule(newAccessRule);
Directory.SetAccessControl(folderPath, dirSecurity);
}
假设我在本地计算机上有一个名为“RootShare”的共享,其中还有另一个文件夹“InsideRootShare”。
场景 1:
当我打电话时,
GiveNTFSPermissions(@"\\sri-devpc\RootShare",
@"domain\username",
FileSystemRights.Write);
共享路径上的继承权限丢失,
场景 2: 当我打电话时,
GiveNTFSPermissions(@"\\sri-devpc\RootShare\InsideRootShare",
@"domain\username",
FileSystemRights.Write);
继承的权限完好无损。
我尝试过使用 FileSystemAccessRule 的不同构造函数,但没有成功。
这种行为背后的原因是什么,以及任何解决方法?
【问题讨论】:
-
您是否尝试过将
InheritanceFlags作为参数的constructor?此外,AddNTFSPermission是一个比GiveNTFSPermissions更合理的名称,因为它尝试向现有访问规则添加新的访问规则。 -
@Nawaz:我尝试了所有的构造函数,结果都一样。谢谢你的建议:)
-
您使用了哪些标志组合?你能看到SetAccessControl前后的访问规则吗?看看它们是否只存在一条规则(刚刚添加)
-
@Nawaz:我尝试过使用 InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,PropagationFlags.None。而且我已经检查了scenario2,它仅相差一个。在scenario1中,我只能看到添加的所有其他都丢失了。如果你想在你的机器上尝试,上面的代码可以按原样执行。
标签: c# permissions