Csharp/CSharp Tutorial/Directory Services/User Add

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Adding new user To The Directory

using System;
using System.DirectoryServices;
public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyObject = new DirectoryEntry();
    MyObject.Path = "LDAP://HMSRevenge/OU=Users,DC=Test,DC=com";
    DirectoryEntries users = MyObject.Children;
    DirectoryEntry NewUser = users.Add("Name", "user");
    NewUser.Properties["company"].Add("Corporation");
    NewUser.Properties["employeeID"].Add("1001");
    NewUser.Properties["userPassword"].Add("Password");
    NewUser.rumitChanges();
    return 0;
  }
}

Add user

using System;
using System.Data;
using System.DirectoryServices;
public class MainClass {
   public static void Main(){
      try{
          DirectoryEntry directoryEntry1 = new DirectoryEntry();
          DirectoryEntry directoryEntry2 = new DirectoryEntry();
     
          directoryEntry1.AuthenticationType = System.DirectoryServices.AuthenticationTypes.Secure;
          directoryEntry1.Path = "WinNT://MSHOME/alien";
     
          directoryEntry2.Path = "WinNT://MSHOME/alien/Users";
     
     
          DirectoryEntry newUser = directoryEntry1.Children.Add( "myUser", "User" );
          newUser.Properties[ "FullName" ].Add( "myFullName" );
          newUser.Properties[ "Description" ].Add( "myDescription" );
          newUser.Invoke( "SetPassword", new object[] { "myPassword", } );
          newUser.Properties[ "PasswordExpired" ].Add( 1 );
          newUser.rumitChanges();
          directoryEntry2.Invoke( "Add", new Object[] { newUser.Path, } );
          directoryEntry2.rumitChanges();
          Console.WriteLine(" added" );
      }catch( Exception exc ) {
          Console.WriteLine( exc.ToString() );
      }
    }
}

List user

using System;
using System.Data;
using System.DirectoryServices;
public class MainClass{
   public static void Main() {
      DirectoryEntry directoryEntry1 = new DirectoryEntry();
      directoryEntry1.AuthenticationType = AuthenticationTypes.Secure;
      directoryEntry1.Path = "WinNT://MSHOME/alien";
      foreach( DirectoryEntry entry in directoryEntry1.Children ) {
        if ( entry.SchemaClassName == "User" ) {
          //Console.WriteLine( entry.Properties[ "Name" ].Value );
          Console.WriteLine( entry.Name );
        }
      }
   }   
}