如何将用户帐户添加到组(成员)
How to add a user account to a group (member of)
我想将 windows 帐户用户添加到群组
我用这个方法:
public bool AddUserToGroup(PrincipalContext ctx, string userId, string groupName)
{
try
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
group.Members.Add(ctx, IdentityType.UserPrincipalName, userId);
group.Save();
return true;
}
catch
{
return false;
}
}
当我将此方法与以下内容一起使用时
PrincipalContext
new PrincipalContext(ContextType.Domain, "Lab.net");
它工作正常。
但是当我使用
PrincipalContext with username and password it have exception
new PrincipalContext(ContextType.Domain, "Lab.net","administrator","P@ssw0rd");
例外情况是:
System.DirectoryServices.AccountManagement.PrincipalOperationException: Information about the domain could not be retrieved (1355).
at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags)
at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()
at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsForestName()
at System.DirectoryServices.AccountManagement.ADUtils.ArePrincipalsInSameForest(Principal p1, Principal p2)
at System.DirectoryServices.AccountManagement.ADStoreCtx.UpdateGroupMembership(Principal group, DirectoryEntry de, NetCred credentials, AuthenticationTypes authTypes)
at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes)
at System.DirectoryServices.AccountManagement.ADStoreCtx.Update(Principal p)
at System.DirectoryServices.AccountManagement.Principal.Save()
我可以使用此 PrincipalContext 创建用户,但我无法将用户加入组。
我这样做:
public bool AddUserToGroup(PrincipalContext ctx, DirectoryEntry userId, string groupName)
{
try
{
//GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
//group.Members.Add(ctx, IdentityType.UserPrincipalName, userId);
//group.Save();
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName);
if (groupPrincipal != null) {
DirectoryEntry entry = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
entry.Invoke("Add", new object[] { userId.Path.ToString() });
userId.CommitChanges();
}
else {
return true;
}
return true;
}
catch
{
return false;
}
}
我想将 windows 帐户用户添加到群组 我用这个方法:
public bool AddUserToGroup(PrincipalContext ctx, string userId, string groupName)
{
try
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
group.Members.Add(ctx, IdentityType.UserPrincipalName, userId);
group.Save();
return true;
}
catch
{
return false;
}
}
当我将此方法与以下内容一起使用时
PrincipalContext
new PrincipalContext(ContextType.Domain, "Lab.net");
它工作正常。
但是当我使用
PrincipalContext with username and password it have exception
new PrincipalContext(ContextType.Domain, "Lab.net","administrator","P@ssw0rd");
例外情况是:
System.DirectoryServices.AccountManagement.PrincipalOperationException: Information about the domain could not be retrieved (1355). at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags) at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() at System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsForestName() at System.DirectoryServices.AccountManagement.ADUtils.ArePrincipalsInSameForest(Principal p1, Principal p2) at System.DirectoryServices.AccountManagement.ADStoreCtx.UpdateGroupMembership(Principal group, DirectoryEntry de, NetCred credentials, AuthenticationTypes authTypes) at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes) at System.DirectoryServices.AccountManagement.ADStoreCtx.Update(Principal p) at System.DirectoryServices.AccountManagement.Principal.Save()
我可以使用此 PrincipalContext 创建用户,但我无法将用户加入组。
我这样做:
public bool AddUserToGroup(PrincipalContext ctx, DirectoryEntry userId, string groupName)
{
try
{
//GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, groupName);
//group.Members.Add(ctx, IdentityType.UserPrincipalName, userId);
//group.Save();
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupName);
if (groupPrincipal != null) {
DirectoryEntry entry = (DirectoryEntry)groupPrincipal.GetUnderlyingObject();
entry.Invoke("Add", new object[] { userId.Path.ToString() });
userId.CommitChanges();
}
else {
return true;
}
return true;
}
catch
{
return false;
}
}