我对 C# 非常陌生,我正在使用 LDAP 1) 验证用户的登录凭据和 2) 为我的应用程序获取有关用户的其他信息。我们的 LDAP 服务器不允许我请求的某些数据匿名提供,所以我必须等到我与用户的完整凭据绑定才能提取数据。但是,即便如此,我也无法获得像 sn 和 givenName 这样的简单字段。使用 JXplorer,我可以看到这些值在匿名连接期间隐藏的位置,但使用完整的用户/SSL/密码组合,我可以看到 JXplorer 中的所有内容。我似乎无法通过我的代码做同样的事情。
如果我在第一个 FindOne() 之后遍历属性,则会找到 9 个属性(没有一个是我要查找的属性)。如果我在第二个FindOne() 之后遍历属性,则只有 4 个属性可用。这两个结果似乎都不受 PropertiesToAdd.Add("...") 的影响。
任何建议,将不胜感激。
public string[] Authenticate(string user, string password)
{
string[] results = new string [2];
//Concatenate serverpath + username + container
//I.e. "LDAP://ldap.disney.com:636/CN=donaldDuck,ou=people,dc=la,dc=disney,dc=com"
DirectoryEntry de = new DirectoryEntry(_ldapserver + "cn=" + user + "," + _topContainer);
//User's password for initial verification
de.Password = password;
//initate anonymous bind
de.AuthenticationType = System.DirectoryServices.AuthenticationTypes.SecureSocketsLayer;
DirectorySearcher searcher = new DirectorySearcher(de);
searcher.SearchScope = System.DirectoryServices.SearchScope.Base;
//Search for first record
SearchResult result = searcher.FindOne();
//Check results
if (result == null) throw new Exception(ERR_NOT_FOUND);
de = result.GetDirectoryEntry();
//Return search results
//results[0] = (string)de.Properties["mail"].Value;
// results[1] = (string)de.Properties["givenName"].Value + " " + (string)de.Properties["sn"].Value;
// Distingushed Name of the found account
string DN = de.Path.Substring(de.Path.ToUpper().IndexOf("CN="));
// Close search connection
de.Close();
// now bind and verify the user's password,
de = new DirectoryEntry(_ldapserver + _topContainer);
de.Username = DN;
de.Password = password;
de.AuthenticationType = System.DirectoryServices.AuthenticationTypes.SecureSocketsLayer;
//Obtain additional information
searcher = new DirectorySearcher(de);
searcher.PropertiesToLoad.Add("sn");
searcher.PropertiesToLoad.Add("givenName");
SearchResult r = searcher.FindOne();
de = r.GetDirectoryEntry();
foreach (string property in de.Properties.PropertyNames)
{
Console.WriteLine("\t{0} : {1} ", property, de.Properties[property][0]);
}
//End obtain additional information
//Validate password
Object obj = de.NativeObject;
de.Close();
//if we made it here, we successfully authenticated
return results;
}