c# - 文件夹权限 - 无法翻译部分或所有标识引用

标签 c#

我想为域用户在远程服务器上设置文件夹 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/

相关文章:

c# - DotNetCore 1.0 MVC 如何在直播中自动重定向到单个域

c# - Selenium 在 WaitForElement 之后抛出 "Element No Longer Valid"

c# - 异步委托(delegate)什么时候应该使用回调模式?

c# - 无法覆盖 Azure Web App 中的服务器证书验证

c# - .NET编译的第三方DLL引用冲突

c# - 包装函数的通用方法

c# - 将数据网格绑定(bind)到另一个数据网格的选定项

c# - NHibernate 缓存问题

c# - 设置 Topshelf 和 Quartz.Net 控制台应用程序时从 Ninject 访问对象

c# - 射线体素相交