Feb
28
从LDAP读取记录,把edmworkstation和displayname这2项内容取出,放在一个dictionary中以备用.edmworkstation是用户的登记计算机名,可能含有多条记录.如果读取失败,则在dictionary中放一个"NotValid=yes"项目.
C# Code Copy Code To Clipboard
- private void GetLDAPInfo()
- {
- try
- {
- DirectoryEntry entry = new DirectoryEntry("LDAP://ldap.xxx.com/o=xxx,c=an");
- entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
- DirectorySearcher searcher = new DirectorySearcher(entry);
- searcher.Filter = "(alias=" + getLoginName() + ')';
- SearchResult result = searcher.FindOne();
- if (result == null)
- {
- LDAPInfo.Add("NotValid", "yes");
- errnum = -40;
- return;
- }
- string path = result.Path;
- path = path.Substring(path.LastIndexOf("/") + 1);
- ResultPropertyCollection p = result.Properties;
- string v = "";
- if (p.Contains("edmworkstation"))
- {
- foreach (var a in p["edmworkstation"])
- {
- v += a.ToString().Substring(0, a.ToString().IndexOf('/')) + ";";
- }
- LDAPInfo.Add("pcnames", v);
- }
- else
- {
- LDAPInfo.Add("NotValid", "yes");
- }
- if (p.Contains("displayName"))
- {
- v = "";
- foreach (var a in p["displayName"])
- {
- v += a.ToString();
- }
- LDAPInfo.Add("displayname",v);
- }
- }
- catch
- {
- LDAPInfo.Add("NotValid", "yes");
- errnum = -40;
- return;
- }
- }
Oct
1
以下代码可以把LDAP上指定ALIAS的人的所有信息查出来
C# Code Copy Code To Clipboard
- public void test(string username,string password)
- {
- DirectoryEntry root = new DirectoryEntry("LDAP://ldap.slb.com/o=slb,c=an");
- root.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
- DirectorySearcher searcher = new DirectorySearcher(root);
- searcher.Filter = "(alias=" + Environment.UserName + ")";
- SearchResultCollection results=searcher.FindAll();
- SearchResult result=searcher.FindOne();
- PropertyCollection p = result.GetDirectoryEntry().Properties;
- foreach (PropertyValueCollection i in p)
- {
- string v="";
- if (i.Count > 1)
- {
- foreach (string ii in i)
- {
- v += ii + "\n";
- }
- }
- else
- {
- v = i.Value.ToString(); ;
- }
- MessageBox.Show(i.PropertyName + ": " + v);
- }
Sep
17
开始时写的:
C# Code Copy Code To Clipboard
- DirectoryEntry root = new DirectoryEntry("ldap://ldap.slb.com:389",username,password);
- root.AuthenticationType = AuthenticationTypes.None;
- DirectorySearcher searcher = new DirectorySearcher(root);
- SearchResultCollection results=searcher.FindAll();
结果总是在FindAll()的地方出错,报告"Unknown Error 0x80005000",后来发现,协议的ldap一定要改成大写才行:
C# Code Copy Code To Clipboard
- DirectoryEntry root = new DirectoryEntry("LDAP://ldap.slb.com:389",username,password);
- root.AuthenticationType = AuthenticationTypes.None;
- DirectorySearcher searcher = new DirectorySearcher(root);
- SearchResultCollection results=searcher.FindAll();
这样写就对了,呵呵



