纪效曾的博客

畅谈技术的海洋,共享信息的博大

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

using System;
using System.Collections.Generic;
using System.Text;

using System.DirectoryServices;//需要添加这个程序集引用先

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetCompanyFromAD(@"chenxizhang"));
            Console.Read();
        }

        public static String GetCompanyFromAD(String username)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://server2003", @"xizhang\administrator", "password");
            DirectorySearcher ds = new DirectorySearcher(de);
            ds.Filter = "(&(objectClass=user)( sAMAccountName=" + username + "))";
            SearchResult sr = ds.FindOne();
            DirectoryEntry resultDE = sr.GetDirectoryEntry();
            return (" " + resultDE.Properties["company"].Value.ToString() + " ").Trim();
        }

    }
}

posted on 2012-08-14 09:58  纪效曾  阅读(117)  评论(0编辑  收藏  举报