반응형

프로젝트 중 어떤 사이트에 사용자 인증을 하기위한 Ldap 인증 해딩하기

 

Ldap Server에 User 인증하기는 총 6가지의 Input 정보가 필요하다.

 

Ldap Url

Ldap ID

Ldap Password

Ldap Domain

 

위 정보는 사용자 관계없이 Ldap 서버에 대한 정보이다.

 

추가적으로

 

User ID

User Password

 

Ldap에 인증하고자 User의 하는 ID와 Password가 필요하다.

 

필요한 참조 Namespace는 아래와 같다.


using System.DirectoryServices;
using System.DirectoryServices.Protocols;

 

아래는 인증하기 위한 구현부, 소스는 부분발췌라 없지만 try, catch하여 Exception의 경우도 Fail 처리

 

string url = textBox1.Text;         //Smaple : LDAP://daumLdapServer.com
string ldapID = textBox2.Text;      //ldap ID
string ldapPw = textBox3.Text;      //ldap Password
string domain = textBox4.Text;      //Sample : daumLdapServer.com
string userID = textBox5.Text;      //user ID
string userPW = textBox6.Text;      //user Password 
				
DirectoryEntry rootEntry = new DirectoryEntry(url, ldapID, ldapPw);

DirectorySearcher search = new DirectorySearcher(rootEntry);

search.Filter = "(&" +
	"(objectClass=user)" + 
	"(CN=" + userID  + 
	"))";

SearchResult result = search.FindOne();

if (result == null)
{
	MessageBox.Show("ldap Fail");
	return;
}

string str = (string)result.Properties["samaccountname"][0];

DirectoryEntry userEntry = new DirectoryEntry(url, str + "@" + domain, userPW);
DirectorySearcher userSearch = new DirectorySearcher(userEntry);
SearchResult userResult = userSearch.FindOne();

if (userResult == null)
{
	MessageBox.Show("user Fail");
	return;
}

MessageBox.Show("SUCCESS");

반응형
Posted by Rainfly
l