Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

UserManager: a class to manipulate local Windows users and groups

0.00/5 (No votes)
2 Aug 2006 1  
UserManager was built to simplify local users and groups manipulation

Introduction

UserManager class contains a set of methods to create/modify local Windows users and groups using System.DirectoryServices namespace. ADSI classes in Directory Services namespace enable programmers to access ADSI objects using System.DirectoryServices namespace.

Usage

By using UserManager class, a developer can use the following methods:

  • AddUser
  • RemoveUser
  • SetUserPassword
  • EnableUser
  • DisableUser
  • AddOptionFlagToUser
  • RemoveOptionFlagToUser
  • UserProperties
  • ListUsersInServer
  • AddGroup
  • GroupProperties
  • AddUserToGroup
  • ListUsersInGroup
  • ListGroupsInServer

The following code shows how to use UserManager class to create a new local user.

    string loginName = "TestUser";
    string loginPwd = "PwdTestUser";
    string loginDescription = "Descr TestUser";
    string defaultGroupName = "Users";

    UserManager um = new UserManager();
    if (!um.AddUser(loginName, loginPwd, loginDescription, defaultGroupName))
        MessageBox.Show(um.ErrorMessage, "Warning", MessageBoxButtons.OK);
    else
        MessageBox.Show("User " + loginName + " created", "Warning", 
                            MessageBoxButtons.OK);

    um = null;

The following is the AddUser code:

public bool AddUser(string LoginName, string LoginPassword,
            string LoginDescription, string defaultGroup)
{
    bool created = false;

    try
    {
        using (DirectoryEntry AD = new
                   DirectoryEntry("WinNT://" + Environment.MachineName + ",
                                computer"))
        {
            bool found = false;

            // check if "LoginName" already exists

            try
            {
                found = AD.Children.Find(LoginName, "user") != null;
            }
            catch
            {
                found = false;
            }

            if (!found)
            {
                // create a new directory entry

                using (DirectoryEntry NewUser =
                           AD.Children.Add(LoginName, "user"))
                {
                    // set password

                    NewUser.Invoke("SetPassword", new object[] 
                            { LoginPassword });
                    // set description

                    NewUser.Invoke("Put", new object[] {"Description", 
                                                       LoginDescription});

                    // commit changes

                    NewUser.CommitChanges();

                    // set default options

                    //

                    //     UF_NORMAL_ACCOUNT

                    //     UF_PASSWD_CANT_CHANGE

                    //     UF_DONT_EXPIRE_PASSWD

                    //

                    // you can change this

                    SetDefaultOptionFlags(LoginName);

                    created = true;

                    // if defaultGroup is not null

                    // try to add new user to it

                    if ((defaultGroup != null) && 
                    (defaultGroup.Trim().Length > 0))
                    {
                        DirectoryEntry grp = null;

                        try
                        {
                            // find defaultGroup entry

                            using (grp = AD.Children.Find(defaultGroup, 
                                "group"))
                            {
                                if (grp != null)
                                {
                                    // add new user to group

                                    grp.Invoke("Add", new object[] 
                        { NewUser.Path.ToString() });
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            aErrMsg = ex.Message;
                        }
                    }

                }
            }
            else
                aErrMsg = "User already exists!";

        }
    }
    catch (Exception ex)
    {
        aErrMsg = ex.Message;
    }

    return created;
}

The following image shows the demo program, included in the zip file, which uses the UserManager class to list Users and Groups in your PC.

Conclusion

The download file at the top of this article contains UserManager source code (.NET 2.0 and .NET 1.1 versions) and a demo project (.NET 2.0).

I hope you enjoy this article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here