#region 根据当前登录域账号 获取AD用户姓名和所在OU目录 /// <summary> /// 根据当前登录域账号 获取AD用户姓名和所在OU目录 /// </summary> /// <param name="searchUser">要搜索的当前用户名</param> /// <param name="paths">out返回该用户所在OU目录</param> /// <param name="userName">out 返回该用户的姓名</param> private static void GetADUserNameAndPaths(string searchUser, out List<string> paths, out string userName) { DirectoryEntry entry = null; string name = "*"; List<string> directorys = new List<string>(); try { //读取配置文件...连接登录域服务器 //"LDAP://10.2.17.*" 域服务器地址 //登录名 //密码 entry =// new DirectoryEntry("LDAP://10.2.17.*", "zhuanghao", "123456", AuthenticationTypes.Secure); new DirectoryEntry("LDAP://10.2.17.*", "Administrator", "111aaa...", AuthenticationTypes.Secure); if (entry != null) { DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(&(objectClass=user)(anr=" + searchUser + "))"); // ("(&(objectClass=organizationalUnit)(OU=电力技术部))"); SearchResult searchResult = mySearcher.FindOne(); if (searchResult != null) { string path = searchResult.Path; string[] splits = path.Split(new char[] { ',' }); for (int i = splits.Count() - 1; i >= 0; i--) { if (splits[i].Contains("CN=")) { name = splits[i].Substring(splits[i].IndexOf('=') + 1); } if (splits[i].Contains("OU=")) { directorys.Add(splits[i].Substring(splits[i].IndexOf('=') + 1)); } } //string xing = (string)searchResult.Properties["sn"][0]; //string ming = (string)searchResult.Properties["givenname"][0]; //string name = xing + ming; } } } catch (Exception ex) { throw ex; } finally { //释放资源 entry.Close(); entry.Dispose(); } paths = directorys; userName = name; } #endregion
参考:::::http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#19
http://82831221.blog.163.com/blog/static/4716957820118162045169/
http://www.cnblogs.com/jamsewang/archive/2011/11/03/2234555.html