【问题标题】:Is it possible to create active directory users using the DirectoryServices library?是否可以使用 DirectoryServices 库创建活动目录用户?
【发布时间】:2011-03-27 01:43:40
【问题描述】:

我正在尝试创建一个库来帮助我处理来自 Windows Server 的 Active Directory 的信息。

到目前为止,我已经设法从 Active Directory 列表中获取了一组用户。

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(ConnectionString))
            {
                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);
            }
        }
    }
}

我想知道,有没有办法使用这个库创建用户?

【问题讨论】:

    标签: c# .net active-directory directoryservices


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    Managing Directory Security Principals in the .NET Framework 3.5

    S.DS.AM 命名空间为您提供了很好的强类型类,可以与用户 (UserPrincipal) 和组 (GroupPrincipal) 一起使用。您可以轻松地使用这些对象并检查和设置它们的属性 - 非常漂亮和干净,不用再胡乱处理 DirectoryEntry 及其混乱的 .Properties 之类的东西了。

    基本上,您可以定义域上下文,然后您可以轻松地在 AD 中搜索和查找用户和/或组以及创建新实体:

    // 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();
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易:

    【讨论】:

      【解决方案2】:

      试试

          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();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多