开发者

Is it possible to create active directory users using the DirectoryServices library?

I'm trying to create a library that helps me manipulate information from Windows Server's Active Directory.

So far, I've managed to obtain a collection of users from an Active Directory listing.

namespace SharpDirectory
{
    public class UserSearcher
    {
        private List<User> _users;
        public string ConnectionString { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        /// <summary>
        /// Provides a method of accessing user information from Active Directory.
        /// </summary>
        /// <param name="connectionString">A standard LDAP conncetion string to your active directory server.</param>
        /// <param name="username">User that has sufficient permission level to query Active Directory.</param>
        /// <param name="password">Password for the entered user.</param>
        public UserSearcher(string connectionString, string username, string password)
        {
            ConnectionString = connectionString;
            Username = username;
            Password = password;
        }
        /// <summary>
        /// Find all users in an Active Directory.
        /// </summary>
        /// <returns>A List of User objects.</returns>
        public List<User> FindAllUsers()
        {
            _users = new List<User>();

            if (DomainExists(ConnectionStri开发者_运维问答ng))
            {
                var baseDirectory = new DirectoryEntry(ConnectionString);
                baseDirectory.Username = Username;
                baseDirectory.Password = Password;

                DirectorySearcher searcher = new DirectorySearcher();

                searcher.SearchRoot = baseDirectory;
                searcher.Filter = "(objectCategory=user)";
                searcher.SearchScope = SearchScope.Subtree;

                var userResults = searcher.FindAll();

                foreach (SearchResult user in userResults)
                {
                    var newUser = new User();
                    newUser.Name = user.Properties["name"][0].ToString();
                    newUser.Path = user.Path;

                    _users.Add(newUser);
                }
            }

            return _users;
        }

        private bool DomainExists(string _connectionString)
        {
            try
            {
                if (DirectoryEntry.Exists(_connectionString))
                    return true;
                else
                    return false;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
}

I was wondering, is there a way to create users using this library?


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

The S.DS.AM namespace gives you nice, strongly-typed classes to work with users (UserPrincipal) and groups (GroupPrincipal). You can easily work with those objects and inspect and set their properties - very nice and clean, no more mucking around with DirectoryEntry and its messy .Properties and stuff like that.

Basically, you can define a domain context and then you can easily search for and find users and/or groups in AD as well as create new entities:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// create a user principal object
UserPrincipal user = 
   new UserPrincipal(ctx, "YourUserNameHere", "pass@1w0rd01", true);

// assign some properties to the user principal
user.GivenName = "User";
user.Surname = "MyNew";

// force the user to change password at next logon
user.ExpirePasswordNow();

// save the user to the directory
user.Save();

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


Try

    public void CreateUser(string username)
    {

        if (DomainExists(ConnectionString))
        {
            var baseDirectory = new DirectoryEntry(ConnectionString);

            DirectoryEntry user = baseDirectory.Children.Add("CN=" + username, "user");
            user.Properties["sAMAccountName"].Add(username);
            user.CommitChanges();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜