引用命名空间:
using System.DirectoryServices;
代码如下:
![]()
域帐户判断
public static DirectoryEntry GetDirectoryEntry(string region)
{
DirectoryEntry de = new DirectoryEntry();
//de.Path = "LDAP://OU=Domain,DC=YourDomain,DC=com";
//de.Path = Globals.ASPAuthorizeSystemConfig;
//LDAP是轻量目录访问协议
de.Path = "LDAP://developer.microsfot.com/DC=developer,DC=microsfot,DC=com";
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
public static bool VerifyUser(string username, string pwd, string region)
{
bool bRet = false;
DirectoryEntry de = GetDirectoryEntry(region);
de.Username = username;
de.Password = pwd;
//密码经过散列
string strSearch = "(SAMAccountName=" + username + ")";
DirectorySearcher deSearcher = new DirectorySearcher(de, strSearch);
deSearcher.SearchScope = SearchScope.Subtree;
try
{
SearchResult sr = deSearcher.FindOne();
bRet = true;
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
return bRet;
}
CN,OU,DC都是LDAP连接服务器的端字符串中的区别名称(DN,distinguished name);
LDAP连接服务器的连接字串格式为:ldap://servername/DN
其中DN有三个属性,分别是CN,OU,DC
LDAP是一种通讯协议,如同HTTP是一种协议一样的!
在 LDAP 目录中。
DC (Domain Component)
CN (Common Name)
OU (Organizational Unit)
An LDAP 目录类似于文件系统目录. 下列目录:
DC=redmond,DC=wa,DC=microsoft,DC=com
如果我们类比文件系统的话,可被看作如下文件路径:
Com\Microsoft\Wa\Redmond
例如:CN=test,OU=developer,DC=domainname,DC=com
在上面的代码中cn=test 可能代表一个用户名, ou=developer 代表一个active directory中的 组织单位。这句话的含义可能就是说明test这个对象处在domainname. com域的developer组织单元中。