Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加“;每个人”;文件夹的网络共享权限_C#_Security_Networking_Windows 7_Share - Fatal编程技术网

C# 添加“;每个人”;文件夹的网络共享权限

C# 添加“;每个人”;文件夹的网络共享权限,c#,security,networking,windows-7,share,C#,Security,Networking,Windows 7,Share,注意:请不要因为标题与其他类似而忽略 我正在尝试共享Windows 7计算机上的文件夹。我想通过C#给每个人完全的权限 我在其他页面上看过几篇文章,包括这里,告诉我如何做。但和其他人一样,它对我不起作用。下面是一段摘自 在调用上述代码之前,共享文件夹已经完成。以下图像是我得到的结果: 到目前为止,一切顺利。但在下一张图片中,您将看到剩下的两个复选框仍然未选中 请问我错过了什么 谢谢 编辑:下面是用于实际共享的代码 private static void QshareFolder(st

注意:请不要因为标题与其他类似而忽略

我正在尝试共享Windows 7计算机上的文件夹。我想通过C#给每个人完全的权限

我在其他页面上看过几篇文章,包括这里,告诉我如何做。但和其他人一样,它对我不起作用。下面是一段摘自

在调用上述代码之前,共享文件夹已经完成。以下图像是我得到的结果:

到目前为止,一切顺利。但在下一张图片中,您将看到剩下的两个复选框仍然未选中

请问我错过了什么

谢谢

编辑:下面是用于实际共享的代码

    private static void QshareFolder(string FolderPath, string ShareName, string Description)
    {
        try
        {
            ManagementClass managementClass = new ManagementClass("Win32_Share");
            ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
            ManagementBaseObject outParams;

            inParams["Description"] = Description;
            inParams["Name"] = ShareName;
            inParams["Path"] = FolderPath;
            inParams["MaximumAllowed"] = null;
            inParams["Password"] = null;
            inParams["Access"] = null;
            inParams["Type"] = 0x0; // Disk Drive

            // Invoke the method on the ManagementClass object
            outParams = managementClass.InvokeMethod("Create", inParams, null);

            // Check to see if the method invocation was successful
            if ((uint) (outParams.Properties["ReturnValue"].Value) != 0)
            {
                throw new Exception("Unable to share directory.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "error!");
        }
    }

共享和基础文件夹的权限是分开的-您的代码设置了文件/文件夹的ACL。。。因此,您缺少在网络共享本身上设置ACL的部分

当最终通过共享访问文件时,可以获得文件和共享的最小权限


我不知道如何在共享上设置ACL,但是这里有一个相关的C++问题,它可能是如何设置共享权限的好的观点:.< /p> 实际上我的问题与您相反,您的第一个代码片段为我解决了它。您的实现缺少SecurityDescriptor

 private static ManagementObject GetSecurityDescriptor()
 {
            ManagementObject Trustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
            Trustee["SID"] = GetWellKnwonSid(WellKnownSidType.WorldSid);
            Trustee["Name"] = "Everyone";

            ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);
            userACE["AccessMask"] = 2032127;//Full access
            userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit;
            userACE["AceType"] = AceType.AccessAllowed;
            userACE["Trustee"] = Trustee;

            ManagementObject secDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);
            secDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT 
            secDescriptor["DACL"] = new object[] { userACE };
            secDescriptor["Group"] = Trustee;
            return secDescriptor;
  }

private static byte[] GetWellKnwonSid(WellKnownSidType SidType)
{
      SecurityIdentifier Result = new SecurityIdentifier(SidType, null);
      byte[] sidArray = new byte[Result.BinaryLength];
      Result.GetBinaryForm(sidArray, 0);

      return sidArray;
}

您必须将Win32_共享实例分配给Access属性

我添加了用于执行上述共享的代码。这就是我需要修改的吗?是的。注意:你需要两个片段——你已经拥有的ACL和共享的ACL。谢谢链接,但是C++信息对我没有多大帮助。
 private static ManagementObject GetSecurityDescriptor()
 {
            ManagementObject Trustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
            Trustee["SID"] = GetWellKnwonSid(WellKnownSidType.WorldSid);
            Trustee["Name"] = "Everyone";

            ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);
            userACE["AccessMask"] = 2032127;//Full access
            userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit;
            userACE["AceType"] = AceType.AccessAllowed;
            userACE["Trustee"] = Trustee;

            ManagementObject secDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);
            secDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT 
            secDescriptor["DACL"] = new object[] { userACE };
            secDescriptor["Group"] = Trustee;
            return secDescriptor;
  }

private static byte[] GetWellKnwonSid(WellKnownSidType SidType)
{
      SecurityIdentifier Result = new SecurityIdentifier(SidType, null);
      byte[] sidArray = new byte[Result.BinaryLength];
      Result.GetBinaryForm(sidArray, 0);

      return sidArray;
}