C# AD登入验证

1.添加引用System.DirectoryServices

2.using System.DirectoryServices;

private static DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry entry = null;
            try
            {
                entry = new DirectoryEntry("LDAP://192.168.10.5:389/DC=ecera,DC=com,DC=tw", "customer", "0511@sre", AuthenticationTypes.Secure);
            }
            catch (Exception ex)
            {

            }
            return entry;
        }
private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryEntry entry = GetDirectoryObject();
            DirectorySearcher search = new DirectorySearcher(entry);
	    //这里的customer是登入账号
            search.Filter = "(SAMAccountName=" + "customer" + ")";
            try
            {
                SearchResult result = search.FindOne();
                if (result != null)
                {
                     MessageBox.Show("成功");
                     MessageBox.Show(result.Path.ToString());
                }
            }
            catch(Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            
        }


 

3.特别要注意AD连接字串的LDAP必须大写,否则会出现未知错误

4.当登入AD的账号密码不匹配时会出现未知的用户名或密码错误。

5.在asp.net里也测试正常通过。

 

你可能感兴趣的:(LDAP,LDAP,LDAP,ad验证)