2018/11/19 - [프로그램 자료/Java & Spring] - [Java] Active Directory 정보 가져오기 AD
대충 윈폼으로 했더니 예쁘지 않다.
콘솔 프로그램으로 해서 main 함수로 했으면 예쁘게 나왔을 꺼 같다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AD_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetMember();
}
private void GetActiveDirectoryUser()
{
/* Connection to Active Directory */
string sFromWhere = "LDAP://127.0.0.1:389";
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "motolies.com\\userId", "passWord");
/* To find all the users member of groups "Grp1" :
* Set the base to the groups container DN; for example root DN (dc=societe,dc=fr)
* Set the scope to subtree
* Use the following filter :
* (member:1.2.840.113556.1.4.1941:=CN=Grp1,OU=MonOu,DC=X)
*/
DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
dsLookFor.Filter = "(&(objectCategory=user)(givenname=이름*))";
dsLookFor.SearchScope = SearchScope.Subtree;
// 가져올 항목을 필터링 하려면 다음의 주석을 푼다
/*
dsLookFor.PropertiesToLoad.Add("cn");
dsLookFor.PropertiesToLoad.Add("name");
dsLookFor.PropertiesToLoad.Add("samaccountname");
*/
SearchResultCollection srcUsers = dsLookFor.FindAll();
/* Just show each user */
foreach (SearchResult srcUser in srcUsers)
{
Console.WriteLine("{0}", srcUser.Path);
Console.WriteLine("{0}", srcUser.Properties["adspath"][0]);
Console.WriteLine("{0}", srcUser.Properties["cn"][0]);
Console.WriteLine("{0}", srcUser.Properties["name"][0]);
Console.WriteLine("{0}", srcUser.Properties["samAccountName"][0]);
}
}
}
}