我想为域用户在远程服务器上设置文件夹 ACL,但总是收到以下错误消息:
Some or all identity references could not be translated
我做错了什么?
这是我的代码:
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
将设置权限,但使用“未知帐户”
有人可以帮忙吗...
提前致谢。
最佳答案
改进 HeonAle 的答案:
.NET 中未定义 GetPrincipalBySamAccountName() 方法。
因此,我们需要一种方法来获取具有 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 );
关于c# - 文件夹权限 - 无法翻译部分或所有标识引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719035/