2009-10-07 123 views
5

我有一個程序爲用戶輸出創建安全目錄。這工作正常,但我在其中創建的文件(或複製到它)結束了只有管理員訪問。文件權限不會繼承目錄權限

DirectoryInfo outputDirectory = 
      baseOutputDirectory.CreateSubdirectory(outputDirectoryName, 
      GetDirectorySecurity(searchHits.Request.UserId)); 

... 

private DirectorySecurity GetDirectorySecurity(string owner) 
{ 
    const string LOG_SOURCE = "GetDirectorySecurity"; 
    DirectorySecurity ds = new DirectorySecurity(); 

    System.Security.Principal.NTAccount ownerAccount = 
     new System.Security.Principal.NTAccount(owner); 

    ds.SetOwner(ownerAccount); 

    ds.AddAccessRule(
     new FileSystemAccessRule(owner, 
     FileSystemRights.FullControl, 
     AccessControlType.Allow)); 

    //AdminUsers is a List<string> that contains a list from configuration 
    // That represents the admins who should be allowed 
    foreach (string adminUser in AdminUsers) 
    { 
     ds.AddAccessRule(
      new FileSystemAccessRule(adminUser, 
      FileSystemRights.FullControl, 
      AccessControlType.Allow)); 
    } 
    return ds; 
} 

/// <summary> 
/// This method copies any static supporting files, such as javascripts 
/// </summary> 
/// <param name="outputDirectory"></param> 
private void CopySupportingFiles(DirectoryInfo outputDirectory) 
{ 
    foreach (FileInfo file in SupportingFiles) 
    { 
     file.CopyTo(
      Path.Combine(outputDirectory.FullName, file.Name)); 
    } 
} 

等,等,等

我在做什麼錯?爲什麼權限級聯?

回答

3

看起來你應該設置InheritanceFlagsPropagationFlags當設置DirectorySecurity(我相信它會覆蓋你手動設置的任何東西)。

private DirectorySecurity GetDirectorySecurity(string owner) 
{ 
    const string LOG_SOURCE = "GetDirectorySecurity"; 
    DirectorySecurity ds = new DirectorySecurity(); 

    System.Security.Principal.NTAccount ownerAccount = 
     new System.Security.Principal.NTAccount(owner); 

    ds.SetOwner(ownerAccount); 

    ds.AddAccessRule(
     new FileSystemAccessRule(owner, 
     FileSystemRights.FullControl, 
     InheritanceFlags.ObjectInherit, 
     PropagationFlags.InheritOnly, 
     AccessControlType.Allow)); 

    //AdminUsers is a List<string> that contains a list from configuration 
    // That represents the admins who should be allowed 
    foreach (string adminUser in AdminUsers) 
    { 
     ds.AddAccessRule(
      new FileSystemAccessRule(adminUser, 
      FileSystemRights.FullControl, 
      InheritanceFlags.ObjectInherit, 
      PropagationFlags.InheritOnly, 
      AccessControlType.Allow)); 
    } 
    return ds; 
} 
+0

這很有道理,讓我試試吧。 – 2009-10-07 16:53:53