[英]Set permissions for directory and also child folders
我的C#代碼創建了一個用戶,創建了共享文件夾,並在此文件夾上設置了用戶權限,
現在,如果我有以下文件夾:
A
|_B
|_C
|_D
然后,如果我為文件夾A創建共享,則它僅共享A,而不共享B,C,D。
我的疑問:如何啟用繼承? 我的意思是也要共享B,C,D。
我發現這種代碼和平無事,但它無能為力。
這是我的完整代碼:
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);
共享是整個目錄樹的共享,如果父目錄是共享的,則所有后代文件夾也是共享的。
但是共享和文件夾ACL仍然適用。
如果您無法通過共享看到A
的子代,則檢查共享和文件夾權限。 特別是用於訪問共享需要身份讀取訪問這兩個份額, A
看內容A
。
理查德對這個問題的評論是正確的,也是最重要的信息。 通常,您不需要單獨共享子文件夾(僅在非常特殊的情況下)。
此外,對於“同時檢查共享和文件夾權限”,他的答案也可以
代碼中有問題。 共享入口點的NTFS-ACL可能設置錯誤或至少是非標准的,並且可能不是RTException想要的。
使用InheritanceFlags.ContainerInherit , PropagationFlags.InheritOnly
(與原始代碼一樣)將導致:
如果刪除了通常的“用戶” /“授權用戶”權限,則新用戶將獲得拒絕訪問錯誤,因為他甚至無法訪問條目目錄。
使用
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None
導致“默認”權限。
這與問題鏈接中提到的完全相同。
Windows ACL /繼承是一個非常復雜的主題,而且也很容易出錯。 有一些細微之處可能會導致意想不到的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.