2011-03-27 2 views
1

Windows Server의 Active Directory에서 정보를 조작하는 데 도움이되는 라이브러리를 만들려고합니다.DirectoryServices 라이브러리를 사용하여 활성 디렉토리 사용자를 만들 수 있습니까?

지금까지 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); 
      } 
     } 
    } 
} 
궁금

, 수있는 방법이 생성이 라이브러리를 사용하여 사용자가? 당신이 .NET 3.5 경우

답변

1

을 시도까지, 당신은 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", "[email protected]", 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의 차종 광고에서 사용자 및 그룹과 놀기가 정말 쉽습니다.

0

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