使用C#创建Active Directory组

4

我正在创建一个用于管理Active Directory的Web应用程序。我想在特定容器中创建一个组。

var groups = new List<Models.Group>();

PrincipalContext ctx = 
 new PrincipalContext(ContextType.Domain, domain, container, userName, password);

GroupPrincipal oGroupPrincipal = new GroupPrincipal(ctx);
oGroupPrincipal.Description = mGroup.GroupName;
oGroupPrincipal.GroupScope = mGroup.GroupScope;
oGroupPrincipal.IsSecurityGroup = mGroup.IsSecurity;
oGroupPrincipal.Save();

但我收到以下错误信息:
无法隐式转换类型 'string' 到 System.DirectoryServices.AccountManagement.GroupScope?'
我不确定如何处理这个问题。当我的列表中的GroupScope是一个字符串对象时,应该如何将其转换为GroupScope对象?
我还收到了这个错误:
在保存之前,必须将SamAccountName或Name分配给此存储中新创建的Principal对象。
2个回答

2

Group scope是一个枚举类型,其值为local、global和universal。看起来您没有将传入的值转换。

尝试将其设置为静态值:

oGroupPrincipal.GroupScope = System.DirectoryServices.AccountManagement.GroupScope.Local;

如果这样能清除错误,那么尝试解析您的输入字符串: ```如果这样能清除错误,那么尝试解析您的输入字符串:```
oGroupPrincipal.GroupScope = (System.DirectoryServices.AccountManagement.GroupScope)Enum.Parse(typeof(System.DirectoryServices.AccountManagement.GroupScope),value);

尝试执行上述操作时,我遇到了以下错误。无法隐式转换类型“object”为“System.DirectoryServices.AccountManagement.GroupScope?”。存在显式转换(是否缺少强制转换?) - Gericke
ah,enum.parse会返回一个对象,你需要进行强制类型转换。我错过了并已经编辑过了。 - Andy March

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接