使用C#创建Active Directory组

时间:2013-06-21 07:33:32

标签: c# active-directory

我正在创建一个用于管理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对象&gt;在保存之前。

2 个答案:

答案 0 :(得分:2)

尝试

GroupPrincipal oGroupPrincipal = new GroupPrincipal(ctx, samAccountName);

oGroupPrincipal.Save(ctx);

Active Directory的代码示例 -

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#
Active Directory With C#

答案 1 :(得分:1)

组范围是一个包含local,global和universal值的枚举,看起来你没有转换传入值。

尝试将其设置为静态值:

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

如果这样可以清除错误,那么请尝试解析传入的字符串:

oGroupPrincipal.GroupScope = (System.DirectoryServices.AccountManagement.GroupScope)Enum.Parse(typeof(System.DirectoryServices.AccountManagement.GroupScope),value);
相关问题