c# - 将安全组添加到文件夹问题/System.Security.Principal.IdentityNotMappedException :

标签 c# security directory system.security

我正在为一个奇怪的问题而苦苦挣扎,我想知道是否有人可以帮助我。 在我的程序的某些时候,我喜欢添加对文件夹具有适当权限的安全组。步骤看起来像那样。

  1. 创建文件夹
  2. 创建域本地安全组
  3. 创建全局安全组
  4. 将全局组添加到本地组
  5. 将域本地安全组添加到文件夹“\domain\dfs\folder”

我从 Microsoft 页面获得了以下代码来执行此操作

 public static void AddDirectorySecurity(string DirectoryName, string Group, FileSystemRights Rights, InheritanceFlags iFlag, PropagationFlags pFlag, AccessControlType ControlType)
{
    // Create a new DirectoryInfo object.
    DirectoryInfo dInfo = new DirectoryInfo(DirectoryName);

    // Get a DirectorySecurity object that represents the  
    // current security settings.
    DirectorySecurity dSecurity = dInfo.GetAccessControl();

    // Add the FileSystemAccessRule to the security settings. 
    dSecurity.AddAccessRule(new FileSystemAccessRule(Group,Rights,iFlag,pFlag,ControlType));

    // Set the new access settings.
    dInfo.SetAccessControl(dSecurity); // 

}

添加过程如下所示:

path = @"\\domain\dfs\folder"

gRDL_RW = "RDL-group-RW"

    AddDirectorySecurity(path, gRDL_RW, FileSystemRights.Modify, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow);

它在我的测试环境中运行良好,但当我想在生产环境中运行它时,出现错误:

************** Exception Text **************
System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.
at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
at System.Security.Principal.NTAccount.Translate(Type targetType)
at System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
at System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
at Program_dev_1.Form1.button1_Click(Object sender, EventArgs e) in  c:\Users\?????????\Documents\Visual Studio 2012\Projects\brite_dev_1\brite_dev_1\Form1.cs:line 191
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

是不是“管理员权限”的问题?我在生产环境中拥有上帝般的权限。 那么这是怎么回事?

我觉得出于某种原因,所有这些用户友好的名称(例如“FileSystemRights.Modify”)都无法翻译。我检查了测试环境和生产环境的语言环境,两者都是一样的。

是否可以使用隐藏在这些用户友好名称后面的原始数字?

也许还有其他方法可以将安全组添加到文件夹?我真正想要的只是一个可行的解决方案。

最佳答案

当尝试将组添加到文件夹时,应用程序崩溃,因为新创建的组不会立即为所有域 Controller 所知。我必须创建程序来检查新创建的组是否已在整个范围内同步,然后再继续执行其余代码。这是不可能的,或者我找不到指定在将组添加到文件夹权限时使用的域 Controller 的方法。

关于c# - 将安全组添加到文件夹问题/System.Security.Principal.IdentityNotMappedException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20820434/

相关文章:

c# - .NET MVC3 图片上传相关错误 : conversion from type 'System.Web.HttpPostedFileWrapper' to type 'Project.Domain.Entities.Image'

c# - 如何在选择查询中添加函数

c# - 使用WebClient时如何绕过系统代理

c# - 尽管输入不同,但从 foreach 循环返回相同的值

Delphi5 目录存在()

c++ - 绕过堆栈保护-缓冲区溢出

javascript - 如何阻止浏览器在选项卡之间共享 session ?

security - HTML5 网络数据库安全

java - 针对不同事件的 Spring Integration 自定义轮询器

windows - 在所有子目录中调用 git init 的 Powershell 脚本