Authenticating an Active Directory user:-
public bool IsUserAuthenticated (string strAdPath, string strDomain, string strUserName, string strPassword)
{
DirectoryEntry directoryEntry = new DirectoryEntry(string strAdPath, string strDomain+@"\"+strUserName,strPassword);
Object obj = directoryEntry.NativeObject;
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearch.Filter ="(SAMAccountName = " + strUserName + ")";
directorySearch.PropertiesToLoad.Add("cn");
SearchResult searchResult = directorySearch.FindOne();
if (searchResult ==null)
{
return false; // not authenticated
}
else
{
return true; // authenticated
}
}
Searching from Active Directory Server:-
public string[] SearchAD(string strLdapPath, string strCN, string strDomainName,string strSearchFilter)
{
ArrayList arrayStrList = new ArrayList()
string[] strSearch= new string[0];
int i = 0;
string strLdapString = strLdapPath+ "/CN="+strCN+";DC="+strDomainName;
DirectoryEntry directoryEntry = new DirectoryEntry (strLdapString);
DirectorySearcher directorySearcher = new DirectorySearcher (directoryEntry);
directorySearch.Filter = strSearchFilter;
foreach (SearchResult sr in directorySearcher.FindAll())
{
arrayStrList.Add(sr.GetDirectoryEntry().Path);
i++;
}
strSearch = (string[])arrayStrList.ToArray(typeof(string));
return strSearch;
}
Reading LDAP properties:-
public string[] GetLdapProperties(string strLdapPath, string strCN, string strDomainName)
{
ArrayList arrayStrList = new ArrayList()
string[] strProperties = new string[0];
int i = 0;
string strLdapString = strLdapPath+ "/CN="+strCN+";DC="+strDomainName;
DirectoryEntry directoryEntry = new DirectoryEntry (strLdapString);
foreach (string strProperty in directoryEntry.Properties.PropertyNames)
{
arrayStrList.Add(strProperty);
i++;
}
strProperties = (string[])arrayStrList.ToArray(typeof(string));
return strProperties;
}
Reference Link:-
http://it.toolbox.com/blogs/programming-life/integrating-your-net-app-with-active-directory-server-8655
https://support.microsoft.com/en-us/kb/316748
https://msdn.microsoft.com/en-us/library/ff649227.aspx
https://msdn.microsoft.com/en-us/library/ff650307.aspx
public bool IsUserAuthenticated (string strAdPath, string strDomain, string strUserName, string strPassword)
{
DirectoryEntry directoryEntry = new DirectoryEntry(string strAdPath, string strDomain+@"\"+strUserName,strPassword);
Object obj = directoryEntry.NativeObject;
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearch.Filter ="(SAMAccountName = " + strUserName + ")";
directorySearch.PropertiesToLoad.Add("cn");
SearchResult searchResult = directorySearch.FindOne();
if (searchResult ==null)
{
return false; // not authenticated
}
else
{
return true; // authenticated
}
}
Searching from Active Directory Server:-
public string[] SearchAD(string strLdapPath, string strCN, string strDomainName,string strSearchFilter)
{
ArrayList arrayStrList = new ArrayList()
string[] strSearch= new string[0];
int i = 0;
string strLdapString = strLdapPath+ "/CN="+strCN+";DC="+strDomainName;
DirectoryEntry directoryEntry = new DirectoryEntry (strLdapString);
DirectorySearcher directorySearcher = new DirectorySearcher (directoryEntry);
directorySearch.Filter = strSearchFilter;
foreach (SearchResult sr in directorySearcher.FindAll())
{
arrayStrList.Add(sr.GetDirectoryEntry().Path);
i++;
}
strSearch = (string[])arrayStrList.ToArray(typeof(string));
return strSearch;
}
Reading LDAP properties:-
public string[] GetLdapProperties(string strLdapPath, string strCN, string strDomainName)
{
ArrayList arrayStrList = new ArrayList()
string[] strProperties = new string[0];
int i = 0;
string strLdapString = strLdapPath+ "/CN="+strCN+";DC="+strDomainName;
DirectoryEntry directoryEntry = new DirectoryEntry (strLdapString);
foreach (string strProperty in directoryEntry.Properties.PropertyNames)
{
arrayStrList.Add(strProperty);
i++;
}
strProperties = (string[])arrayStrList.ToArray(typeof(string));
return strProperties;
}
Reference Link:-
http://it.toolbox.com/blogs/programming-life/integrating-your-net-app-with-active-directory-server-8655
https://support.microsoft.com/en-us/kb/316748
https://msdn.microsoft.com/en-us/library/ff649227.aspx
https://msdn.microsoft.com/en-us/library/ff650307.aspx