AD用户验证的问题
public static DirectoryEntry GetDirectoryObject() //连接AD
{
DirectoryEntry entry = new DirectoryEntry("LDAP://gdepb.gov.cn", "", "", AuthenticationTypes.Secure);
return entry;
}
public string GetOU(string username)
{
string result = string.Empty;
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);
if (user != null)
{
DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry);
if (directoryEntry != null)
{
string[] directoryEntryPath = directoryEntry.Path.Split(',');
foreach (var splitedPath in directoryEntryPath)
{
string[] eleiments = splitedPath.Split('=');
if (eleiments[0].Trim() == "OU")
{
result = username + "-" + eleiments[1].Trim();
break;
}
}
}
}
return result;
}
using(DirectoryEntry entry = new DirectoryEntry("LDAP://gdepb.gov.cn", "用户名", "密码", AuthenticationTypes.Secure))
{
//entry.RefreshCache(); //如果失败将抛异常
DirectorySearcher searcher = new DirectorySearcher(entry, "(
[解决办法]
(objectClass=person)(objectClass=user))");
//用户名或邮箱都可以登陆
//"(&(
------解决方案--------------------
(objectClass=person)(objectClass=user))(
[解决办法]
(cn=" + 用户名 + ")(mail=" + 邮箱 + ")))"
SearchResult rs = searcher.FindOne(); //如果未找到将抛异常
return true;
}