Click here to Skip to main content
15,916,091 members
Home / Discussions / C#
   

C#

 
GeneralRe: Question about adding/removing account Pin
turbosupramk323-Feb-11 11:05
turbosupramk323-Feb-11 11:05 
GeneralRe: Question about adding/removing account Pin
GenJerDan23-Feb-11 11:08
GenJerDan23-Feb-11 11:08 
GeneralRe: Question about adding/removing account Pin
turbosupramk323-Feb-11 11:33
turbosupramk323-Feb-11 11:33 
GeneralRe: Question about adding/removing account Pin
GenJerDan24-Feb-11 3:33
GenJerDan24-Feb-11 3:33 
GeneralRe: Question about adding/removing account Pin
turbosupramk324-Feb-11 5:27
turbosupramk324-Feb-11 5:27 
AnswerRe: Question about adding/removing account Pin
SledgeHammer0123-Feb-11 10:29
SledgeHammer0123-Feb-11 10:29 
GeneralRe: Question about adding/removing account Pin
turbosupramk323-Feb-11 11:07
turbosupramk323-Feb-11 11:07 
AnswerRe: Question about adding/removing account Pin
turbosupramk323-Feb-11 12:12
turbosupramk323-Feb-11 12:12 
Ok ...

Through the help of others here and my own due diligence, this is working now (I don't think you'll need any references added). While searching for an example, I saw many many questions on how to deal with domain accounts and local administrative groups, but no answers so I wanted to post this for googlers to save them some time.


Active directory class


using System;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Security;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
using System.DirectoryServices;
using System.Security.Principal;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using System.Collections;

namespace AD
{
    class ActiveDirectory
    {

        public static void removeUser(string domainValue, string adGroupAccount)
        {
            try
            {
                string path = string.Format("WinNT://{0},computer", Environment.MachineName);
                using (DirectoryEntry dirEntry = new DirectoryEntry(path))
                {
                    DirectoryEntry groupEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName + "/Administrators,group");

                    try
                    {
                        groupEntry.Invoke("Remove", "WinNT://" + domainValue + @"/" + adGroupAccount + ",user");
                        groupEntry.CommitChanges();
                        MessageBox.Show("Account Removed Successfully");
                        groupEntry.Dispose();
                    }
                    catch (Exception cex)
                    {
                        MessageBox.Show(cex.Message + "\n" + cex.StackTrace + "\n" + cex.Source);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }




        public static void addUser(string domainValue, string adGroupAccount)
        {
            try
            {
                string path = string.Format("WinNT://{0},computer", Environment.MachineName);
                using (DirectoryEntry dirEntry = new DirectoryEntry(path))
                {
                    DirectoryEntry groupEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName + "/Administrators,group");

                    try
                    {
                        groupEntry.Invoke("Add", "WinNT://" + domainValue + @"/" + adGroupAccount + ",user");
                        groupEntry.CommitChanges();
                        MessageBox.Show("Account Added Successfully");
                        groupEntry.Dispose();
                    }
                    catch (Exception cex)
                    {
                        MessageBox.Show(cex.Message + "\n" + cex.StackTrace + "\n" + cex.Source);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

        }




        public static string userExists(string userName, string groupName)
        {
 
            string returnValue = "false";

            try
            {
                DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
                DirectoryEntry userGroup = localMachine.Children.Find(groupName, "group");

                object members = userGroup.Invoke("members", null);
                foreach (object groupMember in (IEnumerable)members)
                {
                    DirectoryEntry member = new DirectoryEntry(groupMember);
                    if (member.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        returnValue = "true";
                       break;
                    }
                }
            }
            catch (Exception ex)
            {
                returnValue = "false";
            }
            return (returnValue);
        }
    }
}




Click for an image of the form


Here is the code on how to run it with a simple form that has 3 buttons and 2 textbox input boxes. Put the group name and the domain into the boxes as so. I've hard coded the group I want to search through (Administrators) so you'll have to change that if you want to search through another group. The user exists is not case sensitive.

private void button2_Click(object sender, EventArgs e)
{
    string domain = textBox1.Text;
    string userName = textBox2.Text;
    try
    {
        AD.ActiveDirectory.addUser(domain, userName);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void button3_Click(object sender, EventArgs e)
{
    string domain = textBox1.Text;
    string userName = textBox2.Text;
    try
    {
        AD.ActiveDirectory.removeUser(domain, userName);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void button4_Click(object sender, EventArgs e)
{
    string domain = textBox1.Text;
    string userName = textBox2.Text;
    string result = AD.ActiveDirectory.userExists(userName, "Administrators");
    MessageBox.Show("Exists equals " + result);
}

Questiontimespan Pin
arkiboys23-Feb-11 5:18
arkiboys23-Feb-11 5:18 
AnswerRe: timespan PinPopular
OriginalGriff23-Feb-11 5:22
mveOriginalGriff23-Feb-11 5:22 
GeneralRe: timespan PinPopular
arkiboys23-Feb-11 5:52
arkiboys23-Feb-11 5:52 
GeneralRe: timespan Pin
OriginalGriff23-Feb-11 8:47
mveOriginalGriff23-Feb-11 8:47 
GeneralRe: timespan Pin
Pete O'Hanlon23-Feb-11 10:08
mvePete O'Hanlon23-Feb-11 10:08 
AnswerRe: timespan PinPopular
Luc Pattyn23-Feb-11 6:06
sitebuilderLuc Pattyn23-Feb-11 6:06 
JokeRe: timespan Pin
musefan23-Feb-11 6:16
musefan23-Feb-11 6:16 
GeneralRe: timespan Pin
Keith Barrow23-Feb-11 9:03
professionalKeith Barrow23-Feb-11 9:03 
GeneralRe: timespan Pin
musefan23-Feb-11 9:20
musefan23-Feb-11 9:20 
Questionbest way to inform the user about newly released application.? Pin
vinu.111123-Feb-11 1:09
vinu.111123-Feb-11 1:09 
AnswerRe: best way to inform the user about newly released application.? Pin
Richard MacCutchan23-Feb-11 2:12
mveRichard MacCutchan23-Feb-11 2:12 
AnswerRe: best way to inform the user about newly released application.? Pin
OriginalGriff23-Feb-11 2:22
mveOriginalGriff23-Feb-11 2:22 
AnswerRe: best way to inform the user about newly released application.? Pin
Soulus8323-Feb-11 9:55
Soulus8323-Feb-11 9:55 
GeneralRe: best way to inform the user about newly released application.? Pin
vinu.111124-Feb-11 17:36
vinu.111124-Feb-11 17:36 
QuestionC# info about attaching app to windows window Pin
Aljaz11123-Feb-11 0:44
Aljaz11123-Feb-11 0:44 
AnswerRe: C# info about attaching app to windows window Pin
Richard MacCutchan23-Feb-11 2:10
mveRichard MacCutchan23-Feb-11 2:10 
GeneralRe: C# info about attaching app to windows window Pin
OriginalGriff23-Feb-11 2:24
mveOriginalGriff23-Feb-11 2:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.