C# 文件夹权限-无法翻译某些或所有标识引用

C# 文件夹权限-无法翻译某些或所有标识引用,c#,C#,我想在远程服务器上为域用户设置文件夹ACL,但始终收到以下错误消息: 无法翻译某些或所有标识引用 我做错了什么 这是我的代码: string folderPath = @"\\remoteServer\testDirectory" string accountName = "domainUser" string domainName = "mydomain"; accountName = domainName + "\\" + accountName; //What rights ar

我想在远程服务器上为域用户设置文件夹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
权限将设置为“未知帐户”

有人能帮忙吗


提前谢谢。

我找到了这个问题的解决方案。 必须创建使用要允许的用户SID创建的SecurityIdentifier对象。 请参阅我的解决方案代码

从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。这似乎有效。

改进了HeonAle的答案:

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

所以,我们需要一种方法来获取主体,它具有SID

对于用户:

                // 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 );

如果是英语就好了。