1.添加引用System.DirectoryServices 2.using System.DirectoryServices; [csharp] 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; } [csharp] private void Form1_Load(object sender, EventArgs e) { DirectoryEntry entry = GetDirectoryObject(); DirectorySearcher search = new DirectorySearcher(entry); [csharp] view plaincopy //這裡的customer是登入賬號 [csharp] 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裡也測試正常通過。