我想在远程服务器上为域用户设置文件夹 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
将设置权限,但使用“未知帐户”
有人可以帮忙吗...
提前致谢。
改进 HeonAle 的答案:
.NET 中未定义 GetPrincipalBySamAccountName() 方法。
因此,我们需要一种方法来获取具有 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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)