1. Active Directory에 조직구성단위 추가

using System.DirectoryServices;

DirectorySearcher DSESearcher = new DirectorySearcher();
DirectoryEntries DEntries = DSESearcher.SearchRoot.Children;                
DirectoryEntry ou2 = DEntries.Add("OU=" + OrgName, "organizationalUnit");
ou2.CommitChanges();
ou2.Close();

2. 조직구성단위 열거

DirectorySearcher DSESearcher = new DirectorySearcher();
DirectoryEntries DEntries = DSESearcher.SearchRoot.Children;
List<string> listOU = new List<string>();

foreach (DirectoryEntry de in DEntries)
{
    if (de.Name.CompareTo("OU") > 0)
    {
        listOU.Add(de.Name.Remove(0, 3));//OU=구성단위이름

     }                
}    

3. 조직구성단위에서 사용자 추가

DirectorySearcher DSESearcher = new DirectorySearcher();
string RootDSE;
try
{
    RootDSE = DSESearcher.SearchRoot.Path;
}
catch
{
    System.Windows.MessageBox.Show("Active Directroy를 찾을수 없습니다.");
    return;
}

//조직구성단위 목록을 미리 가지고 와서 콤보박스에서 선택             
RootDSE = RootDSE.Insert(7, "OU="+ comboBoxOU.SelectedValue +",");
DirectoryEntry myDE = new DirectoryEntry(RootDSE);

//선택한 조직구성단위 아래에 사용자 추가

DirectoryEntry usr = myDE.Children.Add("CN=" + textBoxFName.Text + " " + textBoxGName.Text, "user");
 
//계정 - Windows 2000 이전버전 사용자 로그온 이름
usr.Properties["samAccountName"].Value = textBoxLogonName.Text;
//일반 - 표시이름
usr.Properties["displayName"].Value = textBoxFName.Text + " " + textBoxGName.Text;
//일반 - 이름
usr.Properties["givenname"].Value = textBoxGName.Text;
//일반 - 성
usr.Properties["sn"].Value = textBoxFName.Text; //family name
//계정 - 사용자 로그온 이름
usr.Properties["userPrincipalName"].Value = textBoxLogonName.Text + "@xxxxxx.com";
//조직 - 회사
if (textBoxCompany.Text.Length > 0)
    usr.Properties["company"].Value = textBoxCompany.Text;
//일반 - 전화 번호
if (textBoxTelNum.Text.Length > 0)
    usr.Properties["telephoneNumber"].Value = textBoxTelNum.Text;
//조직 - 직함
if (textBoxTitle.Text.Length > 0)
    usr.Properties["title"].Value = textBoxTitle.Text;
//조직 - 부서
if (textBoxDepartment.Text.Length > 0)
    usr.Properties["department"].Value = textBoxDepartment.Text;
//일반 - 전자 메일
if (textBoxMail.Text.Length > 0)
    usr.Properties["mail"].Value = textBoxMail.Text;

usr.CommitChanges();

usr.Invoke("SetPassword", textBoxPassword.Text);
//usr.Invoke("SetPassword", new object[] { textBoxPW.Text });

int val = (int)usr.Properties["userAccountControl"].Value;
usr.Properties["userAccountControl"].Value = val & ~0x2; //ADS_UF_NORMAL_ACCOUNT;
usr.CommitChanges();

myDE.Close();
usr.Close();

//(조직구성단위:OU, 사용자:CN)

'프로그래밍 > C#' 카테고리의 다른 글

C#으로 오라클접속  (0) 2012.08.29
C#으로 MySql 접속  (0) 2012.08.28
C#의 객체지향  (0) 2012.05.10
C#의 기본문법  (0) 2012.05.08
Posted by 초코송송이
l