Posted by: R Manimaran | July 8, 2008

Working with Windows Active directory users using C#

Authenticate a Active directory user

 

//Authenticate a Active directory user

        public static bool AuthenticateUser(string userName, string password, string domain)

        {

            bool isValidUser = false;

            try

            {

                DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain, userName, password);

                object nativeObject = entry.NativeObject;

                isValidUser = true;

            }

            catch (DirectoryServicesCOMException ex)

            {

                throw new DirectoryServicesCOMException(ex.Message);

            }

            return isValidUser;

        }

 

 

 

Add an User to Group

        public static void AddUserToGroup(string UserName, string domain)

        {

            try

            {

                DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);

                entry.Properties[“member”].Add(UserName);

                entry.CommitChanges();

                entry.Close();

            }

            catch (DirectoryServicesCOMException ex)

            {

                throw new DirectoryServicesCOMException(“Error while adding user to group “+ ex.Message);

            }

        }

 

Remove User from a Group

        //Remove a User from a Active Directory Group

        public static void RemoveUserFromGroup(string userName, string domain)

        {

            try

            {

                DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);

                entry.Properties[“member”].Remove(userName);

                entry.CommitChanges();

                entry.Close();

            }

            catch (DirectoryServicesCOMException ex)

            {

                throw new DirectoryServicesCOMException(“Error while Remove user “ + ex.Message);

            }

        }

 

Create a User Account

        //Create a userAccount

        public static Guid CreateActiveDirUser(string userName, string password, string domain)

        {

            Guid oGuid = Guid.Empty;

            try

            {               

                DirectoryEntry entry = new DirectoryEntry(“LDAP://” + domain);

                DirectoryEntry newUser = entry.Children.Add(“CN=” + userName, “user”);

 

                newUser.Properties[“samAccountName”].Value = userName;

                newUser.CommitChanges();

                oGuid = newUser.Guid;

 

                newUser.Invoke(“SetPassword”, new Object[] { password });

                newUser.CommitChanges();

                newUser.Close();

            }

            catch (DirectoryServicesCOMException ex)

            {

                throw new DirectoryServicesCOMException(“Error while creating user “ + ex.Message);

            }

            return oGuid;

        }

 

 

 

 

 

 

About these ads

Responses

  1. I am realy thank for you I found my need
    thank’s again

  2. Really good one.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: