Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:59:10+00:00 2026-06-18T15:59:10+00:00

Okay, my problem right now is we’re trying to write code that will add

  • 0

Okay, my problem right now is we’re trying to write code that will add a user to a different group in our Active Directory. This is the solution we’ve written.

Part of the main method:

string newGroup = "TestDelete";
string userName = result.Properties["cn"][0].ToString();
string adduser = ad.AddToGroup(userName, newGroup);
Console.WriteLine(String.Format("{0} : {1}",userName, adduser)); 

Which calls this method from another class:

public String AddToGroup(string userDn, string groupDn)
{
    try
    {
        DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://OU=" + groupDn + ",DC=blah,DC=blah,DC=blah");
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

        string newUser = "CN=" + userDn + "CN=Members,DC=blah,DC=blah,DC=blah";

        ldapConnection.Invoke("Add", new object[] { newUser });
        ldapConnection.CommitChanges();
        ldapConnection.Close();

        return "Success";
    }
    catch (System.DirectoryServices.DirectoryServicesCOMException E)
    {
        Console.WriteLine("Exception caught:\n\n" + E.ToString());
    }
}

It’s throwing the exception

System.Runtime.InteropServices.COMException (0x80020006): Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
at System.DirectoryServices.DirectoryEntry.InvokeSet(String propertyName, Object[] args)
at adjustUsers.Program.AddToGroup(String userDn, String groupDn) in C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\adjustUsers\Program.cs:line 45
at UserPruning.MainProgram.Main(String[] args) in C:\Users\XXX\Documents\Visual Studio 2010\Projects\UserPruning\UserPruning\MainProgram.cs:line 46

Which, as far as we’ve been able to find indicates a problem with our syntax.

Line 46 is

string adduser = ad.AddToGroup(userName,newGroup)

Line 45 is

ldapConnection.Invoke("Add", new object[] {newUser});

We’ve been trying to rewrite this piece of code for the last day and are still stumped.

Help?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:59:12+00:00Added an answer on June 18, 2026 at 3:59 pm

    If you’re on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on System.DirectoryServices.AccountManagement

    Basically, you can define a domain context and easily find users and/or groups in AD:

    // set up domain context
    using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
    {
        // find your user
        UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
        if(user != null)
        {
            // find the group in question
            GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "TestDelete");
    
            // if found....
            if (group != null)
            {
                // add user to group
                group.Members.Add(user);
                group.Save();
            }
        }
    }
    

    The new S.DS.AM makes it really easy to play around with users and groups in AD!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I have the weirdest problem right now. My code is fine! It
Okay, here is my problem. I have a Service that receives Push notifications. Now,
Okay, so I am trying to compile something right now and I am new
Okay, my problem is that #main_content is the child of #content but it will
Okay, I am writing a function that will allow many different types of bullets
Okay so i have a semi weridish problem with re.sub. Take the following code:
Okay so I am having this problem. I write up a script to be
Okay, I understand what the problem is, I sort of understand constraints in that
Okay guys, basically the problem I'm having is this. I've been assigned to write
Okay, I seem to be having a problem. I'm trying to create a twicker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.