what is LDAP?
The Lightweight Directory Access Protocol (LDAP) (pronounced /ˈɛldæp/) is an application protocol for reading and editing directories over an IP network.[1] A directory is an organized set of records. For example, the telephone directory is an alphabetical list of persons and organizations, with each record having an address and phone number. A directory information tree often follows political, geographic, or organizational boundaries. LDAP directories often use Domain Name System (DNS) names for the highest levels. Deeper inside the directory might appear entries for people, departments, teams, printers, and documents.
How We can implement the Active Directory User Accounts Validation Application?
Step 1:
Create the Sample Solution
step 2:
System.DirectoryServices.dll need to add the Refferance to the Solution
step:
solution Right Click-->Add Refferance --->.net-->
System.DirectoryServices.dll -->ok
step 3:
Create the Class called LdapAuthentication.cs and it's contain
namespace LDAP
{
public class LdapAuthentication
{
private string _path;
private string _filterAttribute;
public LdapAuthentication(string path)
{
_path = path;
}
public bool IsAuthenticated(string domain, string username, string pwd)
{
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{
//Bind to the native AdsObject to force authentication.
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public string GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn=" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
string dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (string)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
}
step 4:
how we can Access LdapAuthentication Class in Login Page?
Sample Code:
string domainName=Environment.UserDomainName;
//Note : if Same domain will update means need to get domain alos input Field
string adPath = "LDAP://"+domainName+"" ;
LdapAuthentication adAuth = new LdapAuthentication(adPath);
bool isAuthenticated = adAuth.IsAuthenticated(domainName, "UserName", "PassWord");
if (isAuthenticated==true)
{
//Login success
}
else
{
//login Failure
}
Thanks
Happy Code
No comments:
Post a Comment