C# 使用C创建Active Directory组#

C# 使用C创建Active Directory组#,c#,active-directory,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 GroupPrinc

我正在创建一个用于管理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();
var groups=newlist();
PrincipalContext ctx=
新PrincipalContext(ContextType.Domain、域、容器、用户名、密码);
GroupPrincipal oGroupPrincipal=新的GroupPrincipal(ctx);
oGroupPrincipal.Description=mGroup.GroupName;
oGroupPrincipal.GroupScope=mGroup.GroupScope;
oGroupPrincipal.IsSecurity组=mGroup.IsSecurity;
oGroupPrincipal.Save();
但我得到了以下错误:

无法将类型“string”隐式转换为 System.DirectoryServices.AccountManagement.GroupScope?“

我不知道该怎么处理。当GroupScope是列表中的对象字符串时,如何将其转换为对象GroupScope

我也得到了这个错误:

保存之前,必须将SamAccountName或Name分配给此存储>中新创建的主体对象

试一试

Active Directory的代码示例-



组作用域是一个具有本地、全局和通用值的枚举,看起来您尚未强制转换传入值

尝试将其设置为静态值:

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?”。存在显式转换(是否缺少强制转换?)ah enum.parse将返回一个对象,您将需要一个我已编辑的、缺少强制转换的对象
oGroupPrincipal.GroupScope = System.DirectoryServices.AccountManagement.GroupScope.Local;
oGroupPrincipal.GroupScope = (System.DirectoryServices.AccountManagement.GroupScope)Enum.Parse(typeof(System.DirectoryServices.AccountManagement.GroupScope),value);