WUGNET, the Windows User Group Network
Your Complete Resource Center for "The Best" in Shareware, Computing Tips and Support, Windows Industry News... and much more!
Home Forums Shareware Windows Tips Hot Offers FREE Newsletters Arcade Contact Us About Partners
Search WUGNET: RSS Feeds RSS Feeds Advertise with WUGNET    |    Shareware eBooks
HomeHome FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Change the group of a user in AD through C#

 
   Home -> Windows Other -> Active Directory RSS
Next:  Outlook express  
Author Message
arunava78

External


Since: May 29, 2007
Posts: 1



(Msg. 1) Posted: Tue May 29, 2007 11:48 pm
Post subject: Change the group of a user in AD through C#
Archived from groups: microsoft>public>win2000>active_directory (more info?)

Hi,

I'm trying to modify an existing AD User (thru C#). I can change the
Name and Password but I'm unable to change the group. I was trying to
remove the user from all groups and then add the user to the specified
group. When I'm trying to remove the user from a group, an error is
coming "The server is unwilling to process the request. (Exception
from HRESULT: 0x80072035) trying to remove a user from group". I'm
giving a code block here:

//change group membership
//first remove user from other groups

ArrayList arlGroups = GetGroups(LoginID, false);
DirectoryEntry oGroupToRemove = new DirectoryEntry();
for (int i = 0; i < arlGroups.Count; i++)
{
string sGroup = arlGroups.ToString();
sGroup = sGroup.Substring(0, sGroup.IndexOf(","));
string[] split = sGroup.Split(new Char[] { '=' });
sGroup = split[1].ToString();
oGroupToRemove = oDirEntry.Children.Find("CN=" + sGroup, "group");
//THE NEXT LINE IS CAUSING THE EXCEPTION
oGroupToRemove.Properties["member"].Remove("CN=" + LoginID);

oGroupToRemove.CommitChanges();
oGroupToRemove.Close();
}
//add member to the given group
DirectoryEntry oGroup = new DirectoryEntry();
oGroup = oDirEntry.Children.Find("CN=" + GroupName, "group");
if (oGroup != null)
{
oGroup.Invoke("Add", new object[] { oUser.Path.ToString() });
}
oGroup.CommitChanges();
oGroup.Close();

Please help me as soon as possible.
Thnx in advance
Back to top
Login to vote
Paul Bergson [MVP-DS]

External


Since: Oct 09, 2006
Posts: 316



(Msg. 2) Posted: Wed May 30, 2007 7:38 am
Post subject: Re: Change the group of a user in AD through C# [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Wrong NewsGroup. You want to go to a coder/scripter NewsGroup.

--
Paul Bergson
MVP - Directory Services
MCT, MCSE, MCSA, Security+, BS CSci
2003, 2000 (Early Achiever), NT

http://www.pbbergs.com

Please no e-mails, any questions should be posted in the NewsGroup
This posting is provided "AS IS" with no warranties, and confers no rights.

wrote in message

> Hi,
>
> I'm trying to modify an existing AD User (thru C#). I can change the
> Name and Password but I'm unable to change the group. I was trying to
> remove the user from all groups and then add the user to the specified
> group. When I'm trying to remove the user from a group, an error is
> coming "The server is unwilling to process the request. (Exception
> from HRESULT: 0x80072035) trying to remove a user from group". I'm
> giving a code block here:
>
> //change group membership
> //first remove user from other groups
>
> ArrayList arlGroups = GetGroups(LoginID, false);
> DirectoryEntry oGroupToRemove = new DirectoryEntry();
> for (int i = 0; i < arlGroups.Count; i++)
> {
> string sGroup = arlGroups.ToString();
> sGroup = sGroup.Substring(0, sGroup.IndexOf(","));
> string[] split = sGroup.Split(new Char[] { '=' });
> sGroup = split[1].ToString();
> oGroupToRemove = oDirEntry.Children.Find("CN=" + sGroup, "group");
> //THE NEXT LINE IS CAUSING THE EXCEPTION
> oGroupToRemove.Properties["member"].Remove("CN=" + LoginID);
>
> oGroupToRemove.CommitChanges();
> oGroupToRemove.Close();
> }
> //add member to the given group
> DirectoryEntry oGroup = new DirectoryEntry();
> oGroup = oDirEntry.Children.Find("CN=" + GroupName, "group");
> if (oGroup != null)
> {
> oGroup.Invoke("Add", new object[] { oUser.Path.ToString() });
> }
> oGroup.CommitChanges();
> oGroup.Close();
>
> Please help me as soon as possible.
> Thnx in advance
>
Back to top
Login to vote
Brian Desmond [MVP]

External


Since: Oct 17, 2005
Posts: 86



(Msg. 3) Posted: Mon Jun 04, 2007 7:49 pm
Post subject: Re: Change the group of a user in AD through C# [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

IIRC the path needs to be LDAP://CN=...

that said post this on directoryprogramming.net


--
Thanks,
Brian Desmond
Windows Server MVP - Directory Services

www.briandesmond.com


wrote in message

> Hi,
>
> I'm trying to modify an existing AD User (thru C#). I can change the
> Name and Password but I'm unable to change the group. I was trying to
> remove the user from all groups and then add the user to the specified
> group. When I'm trying to remove the user from a group, an error is
> coming "The server is unwilling to process the request. (Exception
> from HRESULT: 0x80072035) trying to remove a user from group". I'm
> giving a code block here:
>
> //change group membership
> //first remove user from other groups
>
> ArrayList arlGroups = GetGroups(LoginID, false);
> DirectoryEntry oGroupToRemove = new DirectoryEntry();
> for (int i = 0; i < arlGroups.Count; i++)
> {
> string sGroup = arlGroups.ToString();
> sGroup = sGroup.Substring(0, sGroup.IndexOf(","));
> string[] split = sGroup.Split(new Char[] { '=' });
> sGroup = split[1].ToString();
> oGroupToRemove = oDirEntry.Children.Find("CN=" + sGroup, "group");
> //THE NEXT LINE IS CAUSING THE EXCEPTION
> oGroupToRemove.Properties["member"].Remove("CN=" + LoginID);
>
> oGroupToRemove.CommitChanges();
> oGroupToRemove.Close();
> }
> //add member to the given group
> DirectoryEntry oGroup = new DirectoryEntry();
> oGroup = oDirEntry.Children.Find("CN=" + GroupName, "group");
> if (oGroup != null)
> {
> oGroup.Invoke("Add", new object[] { oUser.Path.ToString() });
> }
> oGroup.CommitChanges();
> oGroup.Close();
>
> Please help me as soon as possible.
> Thnx in advance
>
Back to top
Login to vote
Display posts from previous:   
       Home -> Windows Other -> Active Directory All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum
Categories:
 Windows XP
 Windows Vista
  Windows Other
 Office
 Office Other
 Security

  • Home |
  • Shareware |
  • Windows Tips |
  • Hot Offers |
  • FREE Newsletters |
  • Arcade |
  • Forums |
  • eBooks |
  • About WUGNET |
  • Partners |
  • Contact

  • WUGNET Privacy Policy |
  • Help Forum Terms of Service |
  • Link to WUGNET |
  • IT Support