c# - 为什么在 active directory 组中不能创建为 groupType = Local

标签 c# active-directory ldap distinguishedname

我无法理解为什么在事件目录中将组创建为“本地”组类型不起作用。它抛出以下异常:

 System.DirectoryServices.DirectoryServicesCOMException (0x80072035): The server is unwilling to process the request.

下面是代码示例:

        var parentEntry = new DirectoryEntry(ParentContainer);

        var groupToCreate = parentEntry.Children.Add(this.AttributeType + this.Name, "group");

        groupToCreate.Properties["description"].Add(this.Description);

        groupToCreate.Properties["displayName"].Add(Name);

        groupToCreate.Properties["groupType"].Add((int)GroupType.DomainLocalGroup); --> this line throws error. 


        groupToCreate.CommitChanges();

如果我从 GroupType.DomainLocalGroup 更改为 GroupType.DomainGlobalGroup,一切正常。 任何人都可以让我知道如何摆脱这个问题吗?

enter image description here

最佳答案

According to Microsoft ,这是组类型枚举的定义方式:

  • 1 (0x00000001) 指定由系统创建的组。
  • 2 (0x00000002) 指定一个具有全局范围的组。
  • 4 (0x00000004) 指定具有域本地范围的组。
  • 8 (0x00000008) 指定具有通用范围的组。
  • 16 (0x00000010) 为 Windows Server 授权管理器指定一个 APP_BASIC 组。
  • 32 (0x00000020) 为 Windows Server 授权管理器指定一个 APP_QUERY 组。
  • 2147483648 (0x80000000) 指定一个安全组。如果未设置此标志,则该组为通讯组。

但这也是一个标志枚举——意味着值可以通过将它们相加来组合。所以是的,0x80000004 实际上是一个有效值,表示“域本地安全组”。 (0x4 是域本地通讯组)

但是您必须转换为一个整数(它不会让您将其设置为十六进制值)。我很惊讶你得到的异常是“服务器不愿意处理请求”,因为当我这样做时:

(int) 0x80000004

我得到这个编译器错误:

CS0221: Constant value '2147483652' cannot be converted to a 'int' (use 'unchecked' syntax to override)

那是因为0x80000004的十进制值为2147483652,不适合32位整数。

但是您确实需要给它一个 32 位整数(您不能只转换为 long)。所以你必须按照建议使用unchecked类型转换时:

unchecked((int) 0x80000004)

它给你一个十进制值 -2147483644。

所以你的代码应该是这样的:

groupToCreate.Properties["groupType"].Add(unchecked((int) GroupType.DomainLocalGroup));

关于c# - 为什么在 active directory 组中不能创建为 groupType = Local,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805544/

相关文章:

c# - 如何从 WPF 项目中运行控制台项目

c# - 使用具有多种类型 <T, T> 的泛型注册存储库 c# dotnet core

PowerShell:获取计算机帐户(不是用户帐户)的成员资格信息

c# - Linq to Entities - 针对查询语法与方法语法的预测

active-directory - LDAP 查询 - 查找仅属于 "Domain Users"组的用户

Spring LDAP Context.REFERRAL 遵循

spring - 配置grails spring security ldap插件

java - 用于获取 dn 和所有 cn 数据的 LDAP 查询

ldap - LDAP 有何用途?

c# - 来自 C# 的警报消息