I am creating an Web app for managing an Active Directory. I want to create a group in a certain container.
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();
but I get the following error:
Cannot implicitly convert type 'string' to System.DirectoryServices.AccountManagement.GroupScope?'
I am not sure how to handle this. How should I convert the GroupScope into an object GroupScope while it is a object string in my list?
I also got this error:
SamAccountName or Name must be assigned to a newly-created Principal object in this store > prior to saving.
Group scope is an enum with values of local, global and universal, it looks like you haven't cast the incoming value.
Try setting this to a static value:
oGroupPrincipal.GroupScope = System.DirectoryServices.AccountManagement.GroupScope.Local;
If this clears the error then try parsing your incoming string:
oGroupPrincipal.GroupScope = (System.DirectoryServices.AccountManagement.GroupScope)Enum.Parse(typeof(System.DirectoryServices.AccountManagement.GroupScope),value);
Try
GroupPrincipal oGroupPrincipal = new GroupPrincipal(ctx, samAccountName);
And
oGroupPrincipal.Save(ctx);
Code samples for Active Directory -
http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#
Active Directory With C#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With