62,050
社区成员
发帖
与我相关
我的任务
分享
using System.DirectoryServices;
//srvr = ldap server, e.g. LDAP://domain.com
//usr = user name
//pwd = user password
public bool IsAuthenticated(string srvr, string usr, string pwd)
{
bool authenticated = false;
try
{
DirectoryEntry entry = new DirectoryEntry(srvr, usr, pwd);
object nativeObject = entry.NativeObject;
authenticated = true;
}
catch (DirectoryServicesCOMException cex)
{
//not authenticated; reason why is in cex
}
catch (Exception ex)
{
//not authenticated due to some other exception [this is optional]
}
return authenticated;
}
string LDAPPath = "LDAP://" + domain;
string domainAndUsername = domain + @"\" + UserID;
DirectoryEntry entry = new DirectoryEntry(LDAPPath,
domainAndUsername,
Password);
Object obj;
try
{
// Bind to the native AdsObject to force authentication.
obj = entry.NativeObject;
}
catch (Exception er)
{
Console.WriteLine("Login Failure.");
}
finally
{
entry = null;
obj = null;
}
obj = entry.NativeObject;
string strExchHomeName = usrDirectoryEntry.Properties["msExchHomeServerName"].Value.ToString();