【问题标题】:Folder Permissions - Some or all identity references could not be translated文件夹权限 - 部分或全部身份参考无法翻译
【发布时间】:2022-01-21 22:49:24
【问题描述】:

我想在远程服务器上为域用户设置文件夹 ACL,但总是收到以下错误消息:

部分或全部身份参考无法翻译

我做错了什么?

这是我的代码:

string folderPath = @"\\remoteServer\testDirectory"     
string accountName = "domainUser"
string domainName = "mydomain";
accountName = domainName + "\\" + accountName;
//What rights are we setting?

//set on dir itself
FileSystemAccessRule accessRule = new FileSystemAccessRule(accountName, FileSystemRights.FullControl, AccessControlType.Allow);

DirectoryInfo dInfo = new DirectoryInfo(folderPath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
//dInfo.SetAccessControl(dSecurity);

dSecurity.AddAccessRule(accessRule);`

如果我只输入userName 而不是domainname\username 权限将被设置但“未知帐户”

有人可以帮忙吗...

提前致谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我找到了解决这个问题的方法。 必须创建使用您要允许的用户的 SID 创建的 SecurityIdentifier 对象。 查看我的解决方案代码:

    FileSystemRights Rights;
                
    string folderPath = @"\\remoteServer.domainname\testDirectory";
                
    // Get User from AD with System.DirectoryServices.AccountManagement;
    UserPrincipal user = GetPrinicpalBySamAccountName("userSamAccount"); 
    string usersid = user.Sid.ToString();           
    
    // What rights are we setting?
    SecurityIdentifier secIdentifierSid = new SecurityIdentifier(usersid);
                
    // Set on dir itself
    FileSystemAccessRule accessRule = new FileSystemAccessRule(usersid, FileSystemRights.FullControl, AccessControlType.Allow);
                
    DirectoryInfo dInfo = new DirectoryInfo(folderPath);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
                
    dSecurity.AddAccessRule(accessRule);
    dInfo.SetAccessControl(dSecurity);
    

    https://social.msdn.microsoft.com/Forums/de-DE/682e88c0-e044-46f9-8b5d-55f185e85a1a/directory-acl-berechtigung?forum=visualcsharpde&prof=required

    【讨论】:

    • 如果是英文就好了。
    【解决方案2】:

    改进 HeonAle 的答案:

    GetPrincipalBySamAccountName() 方法未在 .NET 中定义。

    所以,我们需要一种方法来获取具有 SID 的 Principal。

    对于用户:

                    // set up domain context
                    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
                    // find a user
                    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "UserName");
                    string sid = user.Sid.ToString();
    

    对于一个组:

                    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
                    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "GroupName");
                    string sid = group.Sid.ToString();
    

    那么,其余的都是一样的:

    SecurityIdentifier secIdentifierSid = new SecurityIdentifier ( sid );  
    FileSystemAccessRule AccessRule = new FileSystemAccessRule ( secIdentifierSid , FileSystemRights.FullControl, AccessControlType.Allow );
    

    【讨论】:

      【解决方案3】:

      来自 Blen 的链接:

      // Get User from AD with System.DirectoryServices.AccountManagement; 
      UserPrincipal user = GetPrinicpalBySamAccountName ( "userSamAccount" ); 
      string usersid = user.Sid.ToString ();
      
      SecurityIdentifier secIdentifierSid = new SecurityIdentifier ( usersid );  
      FileSystemAccessRule AccessRule = new FileSystemAccessRule ( secIdentifierSid , FileSystemRights.FullControl, AccessControlType.Allow );
      

      我将其更改为使用我们创建的 SecurityIdentifier,而不是简单地发送 SID。这似乎有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-11
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多