2016-08-03 23 views
0

我想將遠程服務器中定義的用戶帳戶組添加到遠程服務器中的文件夾中。無法將遠程服務器中定義的安全組添加到同一服務器的文件夾權限。 「部分或全部身份參考無法翻譯」

public static void SetFolderPermission(string folderPath,string account) 
{ 
    var directoryInfo = new DirectoryInfo(folderPath); 
    var directorySecurity = directoryInfo.GetAccessControl(); 

    var fileSystemRule = new FileSystemAccessRule(account, 
          FileSystemRights.Read, 
          InheritanceFlags.ObjectInherit | 
          InheritanceFlags.ContainerInherit, 
          PropagationFlags.None, 
          AccessControlType.Allow); 

    directorySecurity.AddAccessRule(fileSystemRule); 
    directoryInfo.SetAccessControl(directorySecurity); 
} 

我能夠與domainname\accountname添加帳戶,但我無法添加在遠程服務器上定義的本地帳戶組。

+0

您是否還可以包含發送給此函數的實際(文字)參數? (如'C:\'和'MyMachine \ Sandeep') –

+0

是的,這是一個事實 - **機器本地**用戶和組**不能**被添加到基於服務器的組。 –

+0

我可以將\「servername \ accountname \」這樣的用戶帳戶添加到\\ server1 \ D $ \ folder1中。但是我無法添加像「server1 \ group1」這樣的group1在server1本地的組。 –

回答

0

我能夠通過更改主體上下文到服務器來解決。

public void SetFolderPermission(string folderPath,string account,string server) 
{ 
    var directoryInfo = new DirectoryInfo(folderPath); 
    var directorySecurity = directoryInfo.GetAccessControl(); 
    PrincipalContext pt = new PrincipalContext(ContextType.Machine, server); 

    using (GroupPrincipal val = GroupPrincipal.FindByIdentity(pt, account)) 
{ 

    FileSystemAccessRule fileSystemRule = new FileSystemAccessRule(val.Sid, 
                FileSystemRights.Read, 
                InheritanceFlags.ObjectInherit | 
                InheritanceFlags.ContainerInherit, 
                PropagationFlags.None, 
                AccessControlType.Allow);//var 

    directorySecurity.AddAccessRule(fileSystemRule); 
    directoryInfo.SetAccessControl(directorySecurity); 

    MessageBox.Show("done"); 
} 
} 
相關問題