【问题标题】:Set permissions for directory and also child folders设置目录和子文件夹的权限
【发布时间】:2018-12-10 08:59:35
【问题描述】:

我的 c# 代码创建一个用户,创建共享文件夹并在该文件夹上设置用户权限,

现在如果我有这样的文件夹:

A
|_B
|_C
|_D

如果我为文件夹 A 创建共享,那么它只共享 A 而不会共享 B、C、D。

我的问题:如何启用继承?我的意思是让 B、C、D 也共享。

我找到了this 代码和平,但它什么也没做。

这是我的完整代码:

string uName = "myusername";
string pass = "Rr1234567#";
string path = @"C:\Users\danielf\Desktop\A";
string shareName = "MyShare";
string description = "some description";


PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal user = new UserPrincipal(ctx ,uName  ,pass  , true);
user.PasswordNeverExpires = true;
user.Save();


DirectoryInfo dInfo = new DirectoryInfo(path);
WindowsIdentity id = WindowsIdentity.GetCurrent();
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(uName , FileSystemRights.FullControl , InheritanceFlags.ContainerInherit , PropagationFlags.InheritOnly , AccessControlType.Allow));
        dInfo.SetAccessControl(dSecurity);

        //Gets User SID for share permissions **NotSecurty**
        NTAccount account = new NTAccount(System.Environment.MachineName , uName);
        SecurityIdentifier sid = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier));
        byte[] sidArray = new byte[sid.BinaryLength];
        sid.GetBinaryForm(sidArray , 0);

        ManagementObject Trustee = new ManagementClass("root\\CIMV2" , "Win32_Trustee" , null);
        Trustee["Domain"] = ".";
        Trustee["Name"] = uName;
        Trustee["SID"] = sidArray;

        ManagementBaseObject AdminACE = new ManagementClass(new ManagementPath("Win32_Ace") , null);

        // Add the input parameters.
        AdminACE["AccessMask"] = 2032127;
        AdminACE["AceFlags"] = 3;
        AdminACE["AceType"] = 0;
        AdminACE["Trustee"] = Trustee;

        //Security Descriptor For Share creation Parameter
        ManagementObject secDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor") , null);
        secDescriptor["ControlFlags"] = 4;
        secDescriptor["DACL"] = new object[] { AdminACE };

        ManagementClass classInstance = new ManagementClass("root\\CIMV2" , "Win32_Share" , null);

        // Obtain in-parameters for the method
        ManagementBaseObject inParams = classInstance.GetMethodParameters("Create");

        // Add the input parameters.
        inParams["Name"] = shareName; 
        inParams["Path"] = path;
        inParams["Type"] = 0;
        inParams["Description"] = description;
        inParams["Access"] = secDescriptor;
        inParams["MaximumAllowed"] = null;

        // Execute the method and obtain the return values.
        ManagementBaseObject outParams = classInstance.InvokeMethod("Create" , inParams , null);

【问题讨论】:

  • 注意:只有一个文件夹是共享的,子文件夹是共享的一部分不是单独的共享​​>。要访问B,请使用\\server\SharedA\B。如果你想要\\server\SharedB,那将是一个完全独立的共享。

标签: c# permissions file-permissions user-permissions


【解决方案1】:

共享是整个目录树,如果父目录是共享的,那么所有后代文件夹也是如此。

但共享和文件夹 ACL 仍然适用。

如果您无法通过共享看到A 的子级,请检查共享和文件夹权限。特别是用于访问共享的身份需要对共享和A 的读取权限才能查看A 的内容。

【讨论】:

  • 如果我手动共享 A,那么 B 和 C 的文件夹也有 共享路径,但通过代码没有 共享路径 B,C 仅适用于 A
  • "共享是整个目录树,如果父目录是共享的,那么所有后代文件夹也是如此。"这是不正确的
  • 你检查权限了吗?共享中没有任何内容可以让您创建“没有孩子”(查看net share 命令)。当您在代码中创建时,资源管理器会为共享的 ACL 显示什么?
  • 权限很好,我的意思是它授予了文件夹和子文件夹的权限,但是当我通过代码共享时,子文件夹没有“网络路径”。正如您在屏幕截图中看到的那样。但是如果我手动共享 A,那么 B 就有一个“网络路径”。---->即代码必须缺少某些东西
  • 请在问题中列出共享和 A 的 ACL。如果(例如)用户无权访问 B(因为 A 上的 ACL 未设置为应用于子对象),则它可能不会显示。 (为什么我认为这是一个权限问题:底层共享 API 没有关于是否包含子文件夹的问题。)
【解决方案2】:

理查德对问题的评论是正确的,也是最重要的信息。通常您不需要单独共享子文件夹(仅在非常特殊的情况下)。

此外,他的回答也是“检查共享和文件夹权限”

代码有问题。共享入口点的 NTFS-ACL 可能设置不正确,或者至少是非标准设置,可能不是 RTException 想要的。

使用InheritanceFlags.ContainerInherit , PropagationFlags.InheritOnly(如在原始代码中)导致:

  • 没有用户对条目文件夹的访问权限(因为 ACL 仅继承)
  • 只有子目录继承了这个 ACE,文件没有

如果删除通常的“用户”/“认证用户”权限,新用户会收到拒绝访问错误,因为他甚至无法访问入口目录。

使用

InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None

导致“默认”权限。

这与问题链接中提到的完全相同。

Windows ACL/继承是一个非常复杂的话题,也非常容易出错。有些细微之处可能会导致意想不到的结果。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2012-11-15
    • 2013-07-11
    • 2013-12-22
    • 2010-12-20
    • 2011-05-15
    • 2014-09-28
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多